【问题标题】:How can I get the type of the class that my class-attribute is set to? [duplicate]如何获取我的类属性设置的类的类型? [复制]
【发布时间】:2011-07-09 19:56:50
【问题描述】:

可能重复:
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;
    }

}

注意其中包含注释的行。是否可以获取该类属性所附加的类的类型?

【问题讨论】:

  • 我不确定你的意思,哪个对象的类型?

标签: c# reflection


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-28
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 2018-08-12
    • 2018-02-16
    相关资源
    最近更新 更多