Home >Backend Development >C++ >Can You Dynamically Change Attribute Properties After Assembly Load?

Can You Dynamically Change Attribute Properties After Assembly Load?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-01 00:13:10210browse

Can You Dynamically Change Attribute Properties After Assembly Load?

Dynamically Modifying Attribute Properties

Is it possible to alter attribute parameters after an assembly has been loaded? Consider the following class:

public class UserInfo
{
    [Category("change me!")]
    public int Age
    {
        get;
        set;
    }
    [Category("change me!")]
    public string Name
    {
        get;
        set;
    }
}

Despite being a third-party vendor class (prohibiting code modification), you wish to modify the "change me" category name when binding an instance to a property grid.

Solution:

Attribute instance values can be dynamically modified at runtime. Obtain the attribute instance:

ASCII[] attrs1 = (ASCII[])typeof(MyClass).GetCustomAttributes(typeof(ASCII), false);

Modify its public variables:

attrs1[0].MyData = "A New String";

Create another instance to demonstrate the change:

ASCII[] attrs3 = (ASCII[])typeof(MyClass).GetCustomAttributes(typeof(ASCII), false);
MessageBox.Show(attrs3[0].MyData);

Reference: http://www.vsj.co.uk/articles/display.asp?id=713

The above is the detailed content of Can You Dynamically Change Attribute Properties After Assembly Load?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn