From c44c1b442fde596c1d20c0ad82e4d308f71ec806 Mon Sep 17 00:00:00 2001
From: xcd <834800634@qq.com>
Date: 星期六, 01 八月 2020 13:55:46 +0800
Subject: [PATCH] 部分修改

---
 src/Bro.Common.Model/Helper/PropertyConvertHelper.cs |  147 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 135 insertions(+), 12 deletions(-)

diff --git a/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs b/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs
index 1f56ef7..ab855ea 100644
--- a/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs
+++ b/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs
@@ -1,5 +1,7 @@
-锘縰sing Bro.Common.Factory;
+锘縰sing Autofac;
+using Bro.Common.Factory;
 using Bro.Common.Interface;
+using Bro.Common.Model;
 using Newtonsoft.Json;
 using System;
 using System.Collections;
@@ -112,7 +114,7 @@
         //    set => hash = value;
         //}
 
-        public abstract Hashtable GetConvertHash();
+        public abstract Hashtable GetConvertHash(ITypeDescriptorContext context);
 
         public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
         {
@@ -121,7 +123,7 @@
 
         public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
         {
-            Hash = GetConvertHash();
+            Hash = GetConvertHash(context);
 
             string[] ids = new string[Hash.Values.Count];
             int i = 0;
@@ -182,6 +184,13 @@
                         }
                         return "";
                     }
+
+                    //foreach (DictionaryEntry myDE in Hash)
+                    //{
+                    //    if (myDE.Key.Equals(v))
+                    //        return myDE.Value;
+                    //}
+                    return null;
                 }
             }
             catch (Exception)
@@ -216,7 +225,26 @@
                 {
                     return (value as IComplexDisplay).GetDisplayText();
                 }
-                else
+                else if (value is IEnumerable enumList)
+                {
+                    string display = "";
+                    bool iComplexDisplayMatch = false;
+                    var enumrator = enumList.GetEnumerator();
+                    while (enumrator.MoveNext())
+                    {
+                        if (enumrator.Current is IComplexDisplay d)
+                        {
+                            iComplexDisplayMatch = true;
+                            display += $"{d.GetDisplayText()} ";
+                        }
+                    }
+
+                    if (iComplexDisplayMatch)
+                    {
+                        return display.Trim();
+                    }
+                }
+
                 {
                     return JsonConvert.SerializeObject(value);
                 }
@@ -362,6 +390,100 @@
         public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
         {
             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
@@ -557,7 +679,8 @@
                 TextBox tbox = new TextBox
                 {
                     Dock = DockStyle.Fill,
-                    Multiline = true
+                    Multiline = true,
+                    ScrollBars = ScrollBars.Both,
                 };
                 form.Controls.Add(tbox);
 
@@ -571,7 +694,7 @@
                 }
 
                 form.ShowDialog();
-                List<string> returnStrs = tbox.Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList();
+                List<string> returnStrs = tbox.Text.Split(new char[] { '\r', '\n', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
 
                 switch (typeof(T).Name)
                 {
@@ -624,10 +747,10 @@
             return form;
         }
 
-        //protected override object CreateInstance(Type itemType)
-        //{
-        //    return base.CreateInstance(itemType);
-        //}
+        protected override object CreateInstance(Type itemType)
+        {
+            return base.CreateInstance(itemType);
+        }
 
         //protected override object[] GetItems(object editValue)
         //{
@@ -816,7 +939,7 @@
         }
     }
 
-    public class DeviceTypeConverter : StringConverter 
+    public class DeviceTypeConverter : StringConverter
     {
         public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
         {
@@ -847,7 +970,7 @@
             return new StandardValuesCollection(devices);
         }
     }
-    
+
     public class DeviceInitialConfigEditor<T> : UITypeEditor where T : class, IInitialConfig
     {
         public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)

--
Gitblit v1.8.0