How to get field value in attribute constructor?

There is a StringValueAttribute attribute.  The user can use it to associate string values with an enumeration field.

In the first constructor of StringValueAttribute, two string arguments are passed and can be used to explicitly assigned to _databaseValue and _resourceKey.  This is illustrated in MyEnum.EnumValue1.

My question is in the second constructor with only one string argument.  Since the resource key string is not passed in, I hope to get the enum field value EnumValue2 and then use value.ToString() as the resrouce key.  But how to get this enum field value   This is illustrated in MyEnum.EnumValue2.



    [AttributeUsage(AttributeTargets.Field)]
    public class StringValueAttribute : Attribute
    {
        private string _databaseValue;
        private string _resourceKey;

        public StringValueAttribute(string databaseValue, string resrouceKey)
        {
            _databaseValue = databaseValue;
            _resourceKey = resrouceKey;
        }

        public StringValueAttribute(string databaseValue)
        {
            _databaseValue = databaseValue;
            /* how to get field value */
            _resourceKey = value.ToString();
        }
    }

    public enum MyEnum
    {
        [StringValue("1", "EnumValue1")]
        /* database value = "1", resource key = "EnumValue1" */
        EnumValue1,

        [StringValue("2")]
        /* databae value = "2", resource key = EnumValue2.ToString() = "EnumValue2" */
        EnumValue2

        // other values go here
    }

 


Does anyboday have any idea   Is it possible

Thanks!



Answer this question

How to get field value in attribute constructor?