简体   繁体   中英

How to define Type through Attributes?

In general, there is an attribute Atr over function class A, I want another class B, Type get the class in which it is registered Atr. in my case it should be Type = typeof (A) only without A. I hope you get the idea. Thanks for answers.

Here is a sample code.

public class Atr: Attribute
{
    public Atr()
    {
        DefaultDescription = "hello";
        Console.WriteLine("I am here. I'm the attribute constructor!");
    }

    public String CustomDescription { get; set; }
    public String DefaultDescription { get; set; }

    public override String ToString()
    {
        return String.Format("Custom: {0}; Default: {1}", CustomDescription, DefaultDescription);
    }
}

class B 
{
    public void Laun()
    {
        Type myType = typeof(A);  // хочу получить тоже самое только через Atr
    }
}

class A
{
    [Atr]
    public static void func(int a, int b)
    {
        Console.WriteLine("a={0}  b={1}",a,b);
    }
}

You can use reflection on the Assembly to find all of the classes that have a method in them that are decorated with a given attribute:

Look at the Assembly.GetTypes method ( http://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes%28v=vs.110%29.aspx ) for enumerating all the types in a given assembly.

Look at Type.GetMethods to enumerate all the public methods in a given type ( http://msdn.microsoft.com/en-us/library/424c79hc%28v=vs.110%29.aspx ).

And then finally, look at MemberInfo.CustomAttributes ( http://msdn.microsoft.com/en-us/library/system.reflection.memberinfo.customattributes%28v=vs.110%29.aspx ) to list out all the custom attributes on a given method. CustomAttributes is of type CustomAttributeData, which has property AttributeType which you can compare on.

As you can guess by the number of things you have to loop over (3 nested loops), it will not be easy, fairly convoluted, not to mention SLOW, so you might want to decorate some other aspect of your class, or change around your approach entirely if possible. For example, if you decorate the class itself, it becomes a little easier: Find all classes with an attribute containing a specific property value .

The code to find the class type ends up looking something like this (completely untested):

Type aType = null;
foreach (Type t in Assembly.GetExecutingAssembly().GetTypes()) {
  foreach (MethodInfo mi in t.GetMethods()) {
    foreach (CustomAttributeData cad in mi.CustomAttributes) {
      if (cad.AttributeType == typeof(Atr)) {
        aType = t;
        break;
      }
    } 
  }
}

if (aType == null) {
   // not found
} else {
   // found and aType = typeof(A) in your exmaple
}

Note: You have to make sure you enumerate over the right types (see IsClass property on Type class), but I left that out for clarity.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM