如何获取我的class-attribute设置为的类的类型?

时间:2011-07-09 19:56:50

标签: c# reflection

  

可能重复:
  How to determine the attached type from within a custom 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;
    }

}

注意带有注释的行。是否可以获取此类属性所附加的类的类型,或者不是?

1 个答案:

答案 0 :(得分:3)

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