2011-07-09 38 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

这恐怕是不可能的,但是从类中读取属性的代码将知道它从哪个类读取。所以无论你需要用那个类名来做什么,都应该在那里完成。

相关问题