![](/img/trans.png)
[英]Change Property Attribute Like ReadOnly at Design-Time (PropertyGrid)
[英]Adding an Attribute for property in runtime (for design-time purposes)
我需要在運行時為某些屬性添加一個屬性(出於設計時的目的)。 我知道我可以在課堂上這樣做:
TypeDescriptor.AddAttributes(typeof(MyType), new MyRuntimeAttribute());
但是我找不到任何對屬性執行相同操作的方法。
有什么建議么?
UPD:確切的任務如下:
public class BaseClass {
public BaseClass(string name) { Name = name; }
public Name { get; set; }
}
public class Descendant1: BaseClass {
public Descendant1() : base("Default Name 1") { }
}
...
public class DescendantN: BaseClass {
public DescendantN() : base("Default Name N") { }
}
我希望每個后代在Name屬性中具有各自的DefaultValueAttribute和相應的默認名稱。 而且我不想在每個descentand上硬編碼DefaultValueAttribute :)
您不能動態添加或刪除屬性。 請注意,TypeDescriptor實際上也不會向該類添加屬性:如果在將屬性附加到MyRuntimeAttribute后檢查typeof(MyType).GetCustomAttributes(false)
返回的數組,您會注意到它不屬於該屬性。
既然提到了設計時,那么您可以做的就是動態修改屬性。 那是你真正想做的嗎?
也可以看看:
您還可以提供一個控件設計器,在該設計器中,您可以覆蓋PreFilterProperties。 盡管通常將其用於隱藏屬性,但也可以用於添加屬性。
http://www.codeproject.com/KB/webforms/HidingProperties.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.