【发布时间】:2013-07-27 06:06:38
【问题描述】:
我有这个自定义属性:
[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute
{
public MyAttribute()
{
// I want to get the Test type in here.
// it could be any kind of type that one of its members uses this attribute.
}
}
我在某处使用 MyAtrribute。
public class Test
{
[MyAttribute]
public void MyMethod()
{
//method body
}
public string Name{get;set;}
public string LastName{get;set;}
}
我的问题是,我可以从 MyAttribute 的构造函数中获取测试类的其他成员吗?
感谢您的帮助!
【问题讨论】:
-
你能从
MyAttribute的构造函数中得到MyMethod成员吗? -
@lazyberezovsky 我不确定。我想我可以做 Assembly.GetCallingAssembly().GetTypes().... 但是,问题是我可能在一个程序集中有两个命名空间,它们具有完全相同的类和使用 MyAttribute 的相同方法。
-
@aevitas 你能解释一下副本在哪里吗?您引用的链接完全不同。
-
@Dilshod 没有阅读您发布的作为对我的答案的评论的附加信息,它实际上看起来像是重复的。您应该考虑进行编辑,因为您在编译时不知道要获取成员的类型 - 这将使其成为一个完全不同的问题。
标签: c# reflection attributes custom-attributes