设置自定义属性的属性 [英] Set property of a custom attribute
本文介绍了设置自定义属性的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在运行时更改属性的属性.
示例:
I need to change the property of an attribute at run time.
Example:
public partial class Class1
{
[Attr1("")]
public string ID { get; set; }
}
public class Attr1: Attribute
{
public string Type { get; set;}
public Attr1() { }
public Attr1(string type)
{
Type = type
}
}
测试时:
When testing:
Class1 c = new Class1();
c.ID = "ID1";
PropertyInfo propPK = c.GetType().GetProperty("ID");
object[] attrs = propPK.GetCustomAttributes(typeof(Attr1), false);
if (attrs.Length > 0)
{
object attr = attrs.First();
attr.GetType().GetProperty("Type").SetValue(attr, "Type1" , null);
}
因此,当我按顺序检查[C]对象以检查是否设置了属性的属性时,我看到它为空白...
So when I check the [C] object in order the check if the property of the attribute is set, I see it blank...
推荐答案
对不起,您不能去做.自定义属性是只读的,仅在编译时设置.您最好走另一条路.
Sorry, you cannot do that. Custom attributes are readonly and set only at compile time. You''d be better off going another route.
这篇关于设置自定义属性的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文