Choosing the default value of an Enum type without having to change values

前端 未结 13 2577
迷失自我
迷失自我 2020-12-04 07:46

In C#, is it possible to decorate an Enum type with an attribute or do something else to specify what the default value should be, without having the change the values? The

相关标签:
13条回答
  • 2020-12-04 08:36

    The default value of enum is the enummember equal to 0 or the first element(if value is not specified) ... But i have faced critical problems using enum in my projects and overcome by doing something below ... How ever my need was related to class level ...

        class CDDtype
        {
            public int Id { get; set; }
            public DDType DDType { get; set; }
    
            public CDDtype()
            {
                DDType = DDType.None;
            }
        }    
    
    
        [DefaultValue(None)]
        public enum DDType
        {       
            None = -1,       
            ON = 0,       
            FC = 1,       
            NC = 2,       
            CC = 3
        }
    

    and get expected result

        CDDtype d1= new CDDtype();
        CDDtype d2 = new CDDtype { Id = 50 };
    
        Console.Write(d1.DDType);//None
        Console.Write(d2.DDType);//None
    

    Now what if value is coming from DB .... Ok in this scenario... pass value in below function and it will convertthe value to enum ...below function handled various scenario and it's generic... and believe me it is very fast ..... :)

        public static T ToEnum<T>(this object value)
        {
            //Checking value is null or DBNull
            if (!value.IsNull())
            {
                return (T)Enum.Parse(typeof(T), value.ToStringX());
            }
    
            //Returanable object
            object ValueToReturn = null;
    
            //First checking whether any 'DefaultValueAttribute' is present or not
            var DefaultAtt = (from a in typeof(T).CustomAttributes
                              where a.AttributeType == typeof(DefaultValueAttribute)
                              select a).FirstOrNull();
    
            //If DefaultAttributeValue is present
            if ((!DefaultAtt.IsNull()) && (DefaultAtt.ConstructorArguments.Count == 1))
            {
                ValueToReturn = DefaultAtt.ConstructorArguments[0].Value;
            }
    
            //If still no value found
            if (ValueToReturn.IsNull())
            {
                //Trying to get the very first property of that enum
                Array Values = Enum.GetValues(typeof(T));
    
                //getting very first member of this enum
                if (Values.Length > 0)
                {
                    ValueToReturn = Values.GetValue(0);
                }
            }
    
            //If any result found
            if (!ValueToReturn.IsNull())
            {
                return (T)Enum.Parse(typeof(T), ValueToReturn.ToStringX());
            }
    
            return default(T);
        }
    
    0 讨论(0)
提交回复
热议问题