C# 使用自定义属性获取其成员之一的类

C# 使用自定义属性获取其成员之一的类,c#,reflection,attributes,custom-attributes,C#,Reflection,Attributes,Custom Attributes,我有这个自定义属性: [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 at

我有这个自定义属性:

[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. 
    }
}
我在某处使用我的属性

public class Test
{
    [MyAttribute]
    public void MyMethod()
    {
        //method body
    }

    public string Name{get;set;}

    public string LastName{get;set;}
}
我的问题是,我可以从MyAttribute的构造函数中获得测试类的其他成员吗


谢谢你的帮助

不,你不能。属性的构造函数不可能知道它修饰方法的类型。

不,在属性的构造函数中无法获取任何上下文信息

[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute
{
    public void MyAttributeInvoke(object source)
    {
        source.GetType()
              .GetProperties()
              .ToList()
              .ForEach(x => Console.WriteLine(x.Name));
    }
}
public class Test
{
    public Test()
    {
        GetType().GetMethods()
                 .Where(x => x.GetCustomAttributes(true).OfType<MyAttribute>().Any())
                 .ToList()
                 .ForEach(x => (x.GetCustomAttributes(true).OfType<MyAttribute>().First() as MyAttribute).MyAttributeInvoke(this));
    }

    [MyAttribute]
    public void MyMethod() { }

    public string Name { get; set; }

    public string LastName { get; set; }
}
属性生命周期也与它们关联的项目非常不同(即,只有在有人实际请求属性时才会创建属性)


关于其他类成员的逻辑更好的地方是检查类成员是否具有给定属性的代码(因为此时代码具有关于类/成员的信息)。

在属性构造函数中,您无法获得关于包含由某些属性修饰的成员的类的任何信息,正如我在前面的回答中已经指出的那样

但是我建议了一个解决方案,就是在属性中调用一个方法,而不是使用构造函数,这基本上会得到相同的结果

我已经修改了我以前的答案,用下面的方法解决了你的问题

您的属性现在需要使用以下方法而不是构造函数

[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute
{
    public void MyAttributeInvoke(object source)
    {
        source.GetType()
              .GetProperties()
              .ToList()
              .ForEach(x => Console.WriteLine(x.Name));
    }
}
public class Test
{
    public Test()
    {
        GetType().GetMethods()
                 .Where(x => x.GetCustomAttributes(true).OfType<MyAttribute>().Any())
                 .ToList()
                 .ForEach(x => (x.GetCustomAttributes(true).OfType<MyAttribute>().First() as MyAttribute).MyAttributeInvoke(this));
    }

    [MyAttribute]
    public void MyMethod() { }

    public string Name { get; set; }

    public string LastName { get; set; }
}
您的测试类需要在其构造函数中包含以下代码

[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute
{
    public void MyAttributeInvoke(object source)
    {
        source.GetType()
              .GetProperties()
              .ToList()
              .ForEach(x => Console.WriteLine(x.Name));
    }
}
public class Test
{
    public Test()
    {
        GetType().GetMethods()
                 .Where(x => x.GetCustomAttributes(true).OfType<MyAttribute>().Any())
                 .ToList()
                 .ForEach(x => (x.GetCustomAttributes(true).OfType<MyAttribute>().First() as MyAttribute).MyAttributeInvoke(this));
    }

    [MyAttribute]
    public void MyMethod() { }

    public string Name { get; set; }

    public string LastName { get; set; }
}

你能从
MyAttribute
的构造函数中获得
MyMethod
成员吗?@lazyberezovsky我不确定。我想我可以做Assembly.GetCallingAssembly().GetTypes()。。。。但是,问题是一个程序集中可能有两个名称空间,它们具有完全相同的类和使用MyAttribute的相同方法。可能存在@aevitas的重复项您能解释一下重复项在哪里吗?你所指的链接完全不同。@Dilshod没有阅读你作为对我的答案的评论发布的附加信息,它实际上看起来像一个副本。您应该考虑编辑,因为您不知道在编译时您试图获取成员的类型——这将使它成为一个完全不同的问题。请注意,您的答案似乎与问题无关:“从代码< MyAtgult</代码>的构造函数中获取<代码>测试< /代码>类的其他成员。…我知道如何获取类的方法或其他成员,但我的问题是我还不知道类型。@AlexeiLevenkov它可以做到这一点。但是,什么时候能做到这一点是完全不同的。@aevitas-我想我遗漏了一些东西-您建议如何获取类的
Type
,该类包含与
MyAttribute
相关联的成员?@AlexeiLevenkov不,您确实是正确的,您无法获取使用该属性的外部类型。然而,就目前的情况而言,这个问题可能会有很多误解,我可能会把它解释错。要找到使用某个属性的所有方法,可以通过检查当前(或所有,或特定)程序集中所有类中的所有方法来完成。这就是一些反射库所提供的,以及一些代码注入工具集是如何工作的。此外,您可以将当前的类类型作为属性的参数,但我不明白为什么需要这样做,因为属性已经绑定到方法、属性、字段、类或事件。