如何获取我的类属性设置的类的类型?
可能的重复:
如何从自定义中确定附加类型属性?
我有这个自定义类属性:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕这是不可能的,但是从类中读取属性的代码将知道它正在从哪个类中读取。因此,无论您需要对该类名做什么,都应该在那里完成。
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.