zoukankan      html  css  js  c++  java
  • EnumDescriptionTypeConverter class


    From:http://karmian.org/net-tips-tricks-examples-articles/enum-description-typeconverter
    using
    System; using System.ComponentModel; using System.Globalization;   namespace Karmian.Core.TypeConverters { public class EnumDescriptionTypeConverter : EnumConverter { public EnumDescriptionTypeConverter(Type type) : base(type) { }   public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || TypeDescriptor.GetConverter(typeof(Enum)).CanConvertFrom(context, sourceType); }   public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) return GetEnumValue(EnumType, (string) value); if (value is Enum) return GetEnumDescription((Enum) value); return base.ConvertFrom(context, culture, value); }   public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { return value is Enum && destinationType == typeof(string) ? GetEnumDescription((Enum)value) : (value is string && destinationType == typeof(string) ? GetEnumDescription(EnumType, (string)value) : base.ConvertTo(context, culture, value, destinationType)); }   public static string GetEnumDescription(Enum value) { var fieldInfo = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); }   public static string GetEnumDescription(Type value, string name) { var fieldInfo = value.GetField(name); var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return (attributes.Length > 0) ? attributes[0].Description : name; }   public static object GetEnumValue(Type value, string description) { var fields = value.GetFields(); foreach (var fieldInfo in fields) { var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0 && attributes[0].Description == description) return fieldInfo.GetValue(fieldInfo.Name); if (fieldInfo.Name == description) return fieldInfo.GetValue(fieldInfo.Name); } return description; } } }



    using System;
    using System.ComponentModel;
    using Karmian.Core.TypeConverters;
     
    namespace GoldenSharp.Common
    {
      [TypeConverter(typeof(EnumDescriptionTypeConverter))]
      public enum TargetFramework
      {
        [Description("Default")]
        Default,
        [Description("ISO-1")]
        ISO_1,
        [Description("ISO-2")]
        ISO_2,
        [Description("C# 3.0")]
        CS_3
      }
    }
  • 相关阅读:
    win7下设置smtp的方法
    win7下怎么安装IIS
    python语法笔记(二)
    python语法笔记(一)
    python 的类变量和对象变量
    mysql使用笔记(四)
    mysql使用笔记(三)
    mysql使用笔记(二)
    windows下重新安装TCP/IP协议栈
    c++程序编码
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/3097471.html
Copyright © 2011-2022 走看看