在运行时的类属性变化值 [英] Change values in Class Attribute at runtime

查看:142
本文介绍了在运行时的类属性变化值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样一类

[Attr("Blah",
 Data = "Blah")]
public class Test : SuperClass{}

有没有办法我可以改变在运行时类的实例的属性值?例如,在伪代码

Is there a way I can change the values of the attribute of an instance of the class at runtime? eg in pseudo code

SuperClass test = new Test();
test.Attr.Value = "blah1";
test.Attr.Data = "blah2";



(我有我想改变属性的类的实例,转换为类它扩展)

(I have an instance of the class I want to change the attributes on, cast as a class it extends)

推荐答案

有是属性和对象实例之间不存在隐式连接。只有类和属性之间。最好的办法是寻找在构造函数中缓存的值在对象的属性属性和。当然,这没有任何意义,如果你只是在看测试类,但它有一定道理的,如果的构造父类查找自定义属性与this.GetType()检索到的类型。

There is no implicit connection between attributes and objects instances. Only between the class and the attribute. The best bet would be to look for the attribute in the constructor and "cache" the values in properties on the object. Of course that doesn't make sense if you are only looking at the Test class, but it does make sense if the constructor of the SuperClass looks for the custom attributes on the type retrieved with "this.GetType()".

这篇关于在运行时的类属性变化值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆