locked
[Reflection] Getting the attribute key name for calling GetFields RRS feed

  • Question

  • Hi,

    I have this created method:

     protected void SetPropertyAttributeValue<T>(string propertyName, string attributeName,
                object attributeValue) where T : Attribute
            {
                PropertyDescriptor descriptor = TypeDescriptor
                    .GetProperties(this.GetType())[propertyName];
                T attrib = (T)descriptor.Attributes[typeof(T)];
                Type type =  attrib.GetType();
                FieldInfo fieldInfo = type.GetField(attributeName,BindingFlags.NonPublic | BindingFlags.Instance);
                fieldInfo.SetValue(attrib, attributeValue);
            }

    So when i need to change  something on the attribute at run time so i can just use this method like this:

    SetPropertyAttributeValue<BrowsableAttribute>("MyProperty","browsable", true)

    This is okay because I know the 2nd parameter key string value "browsable" for BrowsableAttribute.

    For other attribute (for example I want to get the key string value for DisplayNameAttribute).... 

    how can i retrieve the Attributes string key to call GetFields:

    BrowsableAttribute = "browsable"

    DescriptionAttribute ="description"

    DisplayNameAttribute= ? 

    Thanks

    Sunday, July 29, 2012 10:25 AM

Answers

  • Hi Fritz,

    Welcome to the MSDN Forum.

    I used this code to find out the name is _displayName:

                PropertyDescriptor descriptor = TypeDescriptor
                    .GetProperties(this.GetType())["MyProperty"];
                DisplayNameAttribute attrib = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)];
                Type type = attrib.GetType();
               foreach ( FieldInfo fi in type .GetFields (BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    Console.WriteLine(fi.Name + "  " + fi.GetValue(attrib));
                }

    Test code:

                PropertyDescriptor descriptor = TypeDescriptor
                    .GetProperties(this.GetType())["MyProperty"];
                DisplayNameAttribute attrib = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)];
                Type type = attrib.GetType();
               foreach ( FieldInfo fi in type .GetFields (BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    Console.WriteLine(fi.Name + "  " + fi.GetValue(attrib));
                }
                SetPropertyAttributeValue<DisplayNameAttribute>("MyProperty", "_displayName", "Abcdefg");
               foreach ( FieldInfo fi in type .GetFields (BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    Console.WriteLine(fi.Name + "  " + fi.GetValue(attrib));
                }

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Thursday, August 9, 2012 2:42 PM
    Monday, July 30, 2012 10:28 AM