2

I have this custom attribute:

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

I am using MyAtrribute in somewhere.

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

    public string Name{get;set;}

    public string LastName{get;set;}
}

My Question is, can I get other members of the test class from the constructor of MyAttribute?

Thanks for the help!

Dilshod
  • 3,189
  • 3
  • 36
  • 67
  • Can you get `MyMethod` member from constructor of `MyAttribute`? – Sergey Berezovskiy Jul 26 '13 at 20:26
  • @lazyberezovsky I am not sure. I think I can do Assembly.GetCallingAssembly().GetTypes().... But, the problem is I may have two namespaces in one assembly that has the exact same class with the same methods that uses MyAttribute. – Dilshod Jul 26 '13 at 20:30
  • possible duplicate of [Get class methods using reflection](http://stackoverflow.com/questions/5475209/get-class-methods-using-reflection) – aevitas Jul 26 '13 at 20:36
  • @aevitas Could you please explain where is the duplicate? The link you are refering is totally different. – Dilshod Jul 26 '13 at 20:42
  • @Dilshod Without reading the additional information you posted as a comment on my answer, it actually looked like a duplicate. You should consider editing in that you don't know the type you are trying to get the members of at compile time - that'll make it an entirely different question. – aevitas Jul 26 '13 at 20:45
  • @aevitas Maybe my english is not good, but if you pay attention to my question, you will get the idea what I am asking. If it was just getting members of Test class, I would say how to get members of Test class. Here is the title of my question "Getting the class which one of its members using custom attribute". – Dilshod Jul 26 '13 at 20:50
  • @Dilshod Edited my answer to match, sorry for misintepreting. – aevitas Jul 26 '13 at 20:53
  • @aevitas no problem. Could you please take that possible duplicate mark from this question? – Dilshod Jul 26 '13 at 20:54

3 Answers3

1

You can not get any info about the class which contains members which are decorated by some attribute, inside the attributes constructor, as I already pointed out in my previous answer here.

Instance to Attribute

But i suggested a solution, which is to invoke a method inside your attribute instead of using the constructor, and this will basically get you the same result.

I have reworked my previous answer a bit to solve your problem in the following way.

Your attribute will now need to use the following method instead of the constructor.

[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute
{
    public void MyAttributeInvoke(object source)
    {
        source.GetType()
              .GetProperties()
              .ToList()
              .ForEach(x => Console.WriteLine(x.Name));
    }
}

And your Test class will need to have the following piece of code inside its constructor.

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

By running the following lines of code, you will notice that you can access both properties from the Test class from your attribute.

class Program
{
    static void Main()
    { 
        new Test();

        Console.Read();
    }
}
Community
  • 1
  • 1
Mario Stopfer
  • 541
  • 3
  • 8
  • To find all methods using a certain attribute, it can be done by going over all methods in all classes in the current (or all, or specific) assemblies. This is what some reflection libraries offer and how some code injection toolsets work. In addition, you can give the current class type as a parameter to the attribute, but I don't see why one would need to do that, the attribute is already bound to a method, property, field, class or event. – Abel Jul 27 '13 at 10:29
0

No you can not. The constructor of your attribute can not possibly know the type in which it decorates a method.

aevitas
  • 3,753
  • 2
  • 28
  • 39
  • 1
    Note that your answer seem to be unrelated to question: "get other members of the `Test` class from the constructor of `MyAttribute`"... – Alexei Levenkov Jul 26 '13 at 20:39
  • I know how to get the methods or other members of the class, but my problem is I don't know the type yet. – Dilshod Jul 26 '13 at 20:40
  • @AlexeiLevenkov It'll do just that. When it'll do that is an entirely different matter, however. – aevitas Jul 26 '13 at 20:41
  • 1
    @aevitas - I guess I'm missing something - how do you suggest to get `Type` of the class which contain a member that `MyAttribute` is associated with? – Alexei Levenkov Jul 26 '13 at 20:44
  • @AlexeiLevenkov No, you are indeed correct, you can't get the outer type of where the attribute is used. However, as it stands, this question is open to a lot of misinterpretation, and I maybe interpreted it wrong. – aevitas Jul 26 '13 at 20:47
0

No, you can't get any context information in attribute's constructor.

Attributes lifetime is also very different from items they are associated with (i.e. they are not created till someone actually asks for attributes).

Better place to have logic about other class member is code that checks if member of a class have given attribute (because at that point the code have information about class/members).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179