Exposing the DescriptionAttribute of Enums from a WCF Service

后端 未结 3 927
予麋鹿
予麋鹿 2021-01-22 11:58

how can i expose description attribute in enum values from service to client or web reference using WCF or

how can i expose enum with description attribute to client

3条回答
  •  时光说笑
    2021-01-22 12:54

    you can do this using reflection. back in the client after you have the Enum value.. try picking up the attribute using GetField() & GetCustomAttributes()

    using System;
    using System.Reflection;
    using System.ComponentModel;
    
    namespace CustomAttributes
    {
        class Program
        {
            static void Main(string[] args)
            {
                Colors n1 = Colors.blue;
    
                object [] attribues = n1.GetType().GetField(n1.ToString()).GetCustomAttributes(true);
                Console.WriteLine((attribues[0] as DescriptionAttribute).Description);
                // WOULD PRINT  "DARK BLUE"
            }
        }
    
        enum Colors
        {
            [Description("DARK BLUE")]
            blue,
            [Description("PLAIN WHITE")]
            white
        }
    }
    

提交回复
热议问题