gpt4 book ai didi

c# - 在运行时更改属性的参数

转载 作者:IT王子 更新时间:2023-10-29 03:43:52 32 4
gpt4 key购买 nike

我不确定是否可以在运行时更改属性的参数?例如,在程序集中我有以下类

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

这是一个由第三方供应商提供的类,我无法更改代码。但是现在我发现上面的描述并不准确,我想在将上述类的实例绑定(bind)到属性网格时将“更改我”类别名称更改为其他名称。

我可以知道怎么做吗?

最佳答案

好吧,你每天都在学习新东西,显然我在撒谎:

What isn’t generally realised is that you can change attribute instance values fairly easily at runtime. The reason is, of course, that the instances of the attribute classes that are created are perfectly normal objects and can be used without restriction. For example, we can get the object:

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

…change the value of its public variable and show that it has changed:

attrs1[0].MyData="A New String";
MessageBox.Show(attrs1[0].MyData);

…and finally create another instance and show that its value is unchanged:

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

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

关于c# - 在运行时更改属性的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51269/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com