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
Release: 2025-01-01 00:13:10
Original
180 people have browsed it

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;
    }
}
Copy after login

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);
Copy after login

Modify its public variables:

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

Create another instance to demonstrate the change:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template