[英]Adding attribute on object during run time not happening
I try to add attributes on a certain object. 我尝试在某个对象上添加属性。 This object can be int, string, List or whatever. 该对象可以是int,string,List等等。
I try to use 我试着用
TypeDescriptor.AddAttributes(object, attrList.ToArray());
but this list of attributes do not show up when I do: 但是当我这样做时,这个属性列表不显示:
object.GetType().GetCustomAttributes(false)
How come? 怎么会?
Best regards, 最好的祝福,
Gabriel Paulsson 加布里埃尔保罗森
Unfortunately this method does not dynamically change the metadata of the type, ultimately it only gives you back a TypeDescriptor which includes the attributes you added. 不幸的是,此方法不会动态更改类型的元数据,最终它只会返回一个包含您添加的属性的TypeDescriptor 。
You need to keep the return value of the AddAttributes method and query from there instead... 您需要保留AddAttributes方法和查询的返回值,而不是......
var myObject = { ... }
var typeDescriptionProvider = TypeDescriptor.AddAttributes(myObject, attrList.ToArray());
var attributes = typeDescriptionProvider.GetTypeDescriptor(myObject).GetAttributes();
You can think of the type descriptor as a union of the type metadata itself (fixed), and any meta data you added at runtime (dynamic). 您可以将类型描述符视为类型元数据本身(已修复)的联合,以及您在运行时添加的任何元数据(动态)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.