Can attributes be added dynamically in C#?

后端 未结 10 821
别跟我提以往
别跟我提以往 2020-11-22 14:32

Is it possible to add attributes at runtime or to change the value of an attribute at runtime?

相关标签:
10条回答
  • 2020-11-22 15:00

    I tried very hard with System.ComponentModel.TypeDescriptor without success. That does not means it can't work but I would like to see code for that.

    In counter part, I wanted to change some Attribute values. I did 2 functions which work fine for that purpose.

            // ************************************************************************
            public static void SetObjectPropertyDescription(this Type typeOfObject, string propertyName,  string description)
            {
                PropertyDescriptor pd = TypeDescriptor.GetProperties(typeOfObject)[propertyName];
                var att = pd.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute;
                if (att != null)
                {
                    var fieldDescription = att.GetType().GetField("description", BindingFlags.NonPublic | BindingFlags.Instance);
                    if (fieldDescription != null)
                    {
                        fieldDescription.SetValue(att, description);
                    }
                }
            }
    
            // ************************************************************************
            public static void SetPropertyAttributReadOnly(this Type typeOfObject, string propertyName, bool isReadOnly)
            {
                PropertyDescriptor pd = TypeDescriptor.GetProperties(typeOfObject)[propertyName];
                var att = pd.Attributes[typeof(ReadOnlyAttribute)] as ReadOnlyAttribute;
                if (att != null)
                {
                    var fieldDescription = att.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
                    if (fieldDescription != null)
                    {
                        fieldDescription.SetValue(att, isReadOnly);
                    }
                }
            }
    
    0 讨论(0)
  • 2020-11-22 15:02

    Attributes are static metadata. Assemblies, modules, types, members, parameters, and return values aren't first-class objects in C# (e.g., the System.Type class is merely a reflected representation of a type). You can get an instance of an attribute for a type and change the properties if they're writable but that won't affect the attribute as it is applied to the type.

    0 讨论(0)
  • 2020-11-22 15:02

    Well, just to be different, I found an article that references using Reflection.Emit to do so.

    Here's the link: http://www.codeproject.com/KB/cs/dotnetattributes.aspx , you will also want to look into some of the comments at the bottom of the article, because possible approaches are discussed.

    0 讨论(0)
  • 2020-11-22 15:06

    This really depends on what exactly you're trying to accomplish.

    The System.ComponentModel.TypeDescriptor stuff can be used to add attributes to types, properties and object instances, and it has the limitation that you have to use it to retrieve those properties as well. If you're writing the code that consumes those attributes, and you can live within those limitations, then I'd definitely suggest it.

    As far as I know, the PropertyGrid control and the visual studio design surface are the only things in the BCL that consume the TypeDescriptor stuff. In fact, that's how they do about half the things they really need to do.

    0 讨论(0)
提交回复
热议问题