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!

How to get field value in attribute constructor?
Michael Braaldey
GetCustomAttribute method is to get the attribute from the type. Here I'm looking for something in the opposite direction. In the attribute constructor, I want to know the value of the type member. That is, in my example above, in the second constructor of StringValueAttribute, I want to get MyEnum.EnumValue2.
Thanks!
pc0416
I would recommend looking into the MSDN documentation for GetCustomAttributes, located at: http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemreflectionmemberinfoclassgetcustomattributestopic.asp
You may want to filter the members somewhat to ignore members such as ToString.
Hope that helps,
Stephen [Microsoft Common Language Runtime: Security - Developer]
http://blogs.msdn.com/stfisher