From 8a3ab64a65da226636743be07c4bee63f50da25d Mon Sep 17 00:00:00 2001 From: patrick.xu <patrick.xu@broconcentric.com> Date: 星期六, 06 三月 2021 11:35:20 +0800 Subject: [PATCH] 1. 解决软件异常崩溃问题 --- src/Bro.Common.Model/Helper/PropertyConvertHelper.cs | 185 ++++++++++++++++++++++++++-------------------- 1 files changed, 105 insertions(+), 80 deletions(-) diff --git a/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs b/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs index 6dc252c..ab855ea 100644 --- a/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs +++ b/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs @@ -1,4 +1,5 @@ -锘縰sing Bro.Common.Factory; +锘縰sing Autofac; +using Bro.Common.Factory; using Bro.Common.Interface; using Bro.Common.Model; using Newtonsoft.Json; @@ -183,6 +184,13 @@ } return ""; } + + //foreach (DictionaryEntry myDE in Hash) + //{ + // if (myDE.Key.Equals(v)) + // return myDE.Value; + //} + return null; } } catch (Exception) @@ -384,6 +392,100 @@ return true; } } + + public class EnumDescriptionConverter<T> : TypeConverter where T : Enum + { + Dictionary<T, string> itemDict = new Dictionary<T, string>(); + public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) + { + typeof(T).GetEnumNames().ToList().ForEach(e => + { + T temp = (T)Enum.Parse(typeof(T), e); + itemDict[temp] = temp.GetEnumDescription(); + }); + + return new StandardValuesCollection(itemDict.Keys); + } + public override bool GetStandardValuesSupported(ITypeDescriptorContext context) + { + return true; + } + + public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) + { + return true; + } + + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) + { + if (sourceType == typeof(String)) + return true; + + return base.CanConvertFrom(context, sourceType); + } + + public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) + { + return base.CanConvertTo(context, destinationType); + } + + public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues) + { + return base.CreateInstance(context, propertyValues); + } + + /// <summary> + /// + /// </summary> + /// <param name="context"></param> + /// <param name="culture"></param> + /// <param name="value">string</param> + /// <returns></returns> + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + { + try + { + foreach (string e in typeof(T).GetEnumNames().ToList()) + { + T temp = (T)Enum.Parse(typeof(T), e); + + if (temp.GetEnumDescription() == value.ToString()) + return temp; + } + } + catch (Exception ex) + { + } + + return base.ConvertFrom(context, culture, value); + } + + /// <summary> + /// + /// </summary> + /// <param name="context"></param> + /// <param name="culture"></param> + /// <param name="value">IOItem</param> + /// <param name="destinationType"></param> + /// <returns></returns> + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (value is T temp) + { + //foreach (KeyValuePair<T, string> pair in itemDict) + //{ + // if (Convert.ToInt32(pair.Key) == Convert.ToInt32(temp)) + // { + // return pair.Value; + // } + //} + + return temp.GetEnumDescription(); + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } #endregion #region UITypeEditor @@ -577,7 +679,8 @@ TextBox tbox = new TextBox { Dock = DockStyle.Fill, - Multiline = true + Multiline = true, + ScrollBars = ScrollBars.Both, }; form.Controls.Add(tbox); @@ -915,84 +1018,6 @@ return base.EditValue(context, provider, value); } - } - - public class IORefrenceItemCollectionEditor : CollectionEditor - { - protected override CollectionForm CreateCollectionForm() - { - var form = base.CreateCollectionForm(); - - var prop = form.GetType().GetField("propertyBrowser", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); - - if (prop != null) - { - if (prop.GetValue(form) is PropertyGrid grid) - { - grid.HelpVisible = true; - grid.ToolbarVisible = false; - } - } - - return form; - } - - //protected override object CreateInstance(Type itemType) - //{ - // return base.CreateInstance(itemType); - //} - - //protected override object[] GetItems(object editValue) - //{ - // return base.GetItems(editValue); - //} - - //protected override object SetItems(object editValue, object[] value) - //{ - // return base.SetItems(editValue, value); - //} - - public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) - { - return base.EditValue(context, provider, value); - } - - public IORefrenceItemCollectionEditor(Type type) : base(type) - { - } - - /// <summary> - /// 闄愬埗涓�娆¢�変竴涓疄渚� - /// </summary> - /// <returns></returns> - protected override bool CanSelectMultipleInstances() - { - return false; - } - - /// <summary> - /// 鎸囧畾鍒涘缓鐨勫璞$被鍨� - /// </summary> - /// <returns></returns> - protected override Type CreateCollectionItemType() - { - return typeof(IORefrenceItem); - } - - protected override string GetDisplayText(object value) - { - if (value is IComplexDisplay) - { - return (value as IComplexDisplay).GetDisplayText(); - } - - return base.GetDisplayText(value); - } - - //protected override void DestroyInstance(object instance) - //{ - // base.DestroyInstance(instance);//閲嶈锛佽嚜鍔ㄥ垹闄ょ粍浠剁殑璁捐鏃朵唬鐮侊紒 - //} } #endregion -- Gitblit v1.8.0