如何获取我的类属性设置的类的类型?

发布于 2024-11-19 04:05:13 字数 971 浏览 1 评论 0原文

可能的重复:
如何从自定义中确定附加类型属性?

我有这个自定义类属性:

[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;
    }

}

请注意其中带有注释的行。是否可以获得该类属性所附加的类的类型,或者不是?

Possible Duplicate:
How to determine the attached type from within a custom attribute?

I have this custom 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;
    }

}

Notice the line with the comment in it. Is it possible to get the type of the class that this class-attribute is attached to, or is it not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-11-26 04:05:13

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

That's not possible, I'm afraid, but the code that reads the attribute from the class will know which class it is reading from. So whatever you need to do with that class name should be done there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文