From 5918194fccdb2a2303e713b8d2f3335243b9e2ef Mon Sep 17 00:00:00 2001
From: wells.liu <wells.liu@broconcentric.com>
Date: 星期二, 07 七月 2020 14:53:13 +0800
Subject: [PATCH] 板卡驱动

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

diff --git a/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs b/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs
index dd48235..b802a92 100644
--- a/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs
+++ b/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs
@@ -184,6 +184,13 @@
                         }
                         return "";
                     }
+
+                    //foreach (DictionaryEntry myDE in Hash)
+                    //{
+                    //    if (myDE.Key.Equals(v))
+                    //        return myDE.Value;
+                    //}
+                    return null;
                 }
             }
             catch (Exception)
@@ -383,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
@@ -916,7 +1017,7 @@
 
             return base.EditValue(context, provider, value);
         }
-    }    
+    }
     #endregion
 
 }

--
Gitblit v1.8.0