2011-07-09 54 views
1

可能重複:
How to determine the attached type from within a custom attribute?如何獲取我的class-attribute設置爲的類的類型?

我有這樣的自定義類屬性:

[AttributeUsage(AttributeTargets.Class)] 
public class ConfigurableClass : Attribute 
{ 

    public Type Control { get; private set; } 
    public bool IsSingleton { get; private set; } 
    public string Name { get; private set; } 

    public ConfigurableClass(bool isSingleton) : this(null, isSingleton) 
    { 
    } 

    public ConfigurableClass(Type control, bool isSingleton) 
    { 
     Control = control; 
     this.IsSingleton = isSingleton; 
     this.Name = ""; //set name to typename of the attached class here? 
    } 

    public ConfigurableClass(Type control, bool isSingleton, string name) : this(control, isSingleton) 
    { 
     this.Name = name; 
    } 

} 

注意與它註釋的行。是否有可能獲得該類屬性附加到的類的類型,還是不是?

+0

我不確定你的意思,哪個對象的類型? – Magnus

回答

3

這恐怕是不可能的,但是從類中讀取屬性的代碼將知道它從哪個類讀取。所以無論你需要用那個類名來做什麼,都應該在那裏完成。

相關問題