简体   繁体   中英

Reflection attribute method

I have an asbtract class and I have classes that are devired from it. I have an attribute called PluginEventAttribute that works like so:

[PluginEventAttribute(PluginEvent.Load)]
public void OnLoad()
{
    Log("Test Plugin loaded!");
}

I want my code to check if there is a method that uses that attribute, and if so, call it with custom parameters. How can I do that in C# winforms?

You just have to enumerate the instance methods and call the method if it has said attribute. Here's a working example (I hope I got your intent correctly) :

using System;
using System.Reflection;

class Program
{
    class MyAttr : Attribute { }

    abstract class Base { };

    class Derived : Base
    {
        [MyAttr]
        public void foo() { Console.WriteLine("foo"); }
        public void bar() { Console.WriteLine("bar"); }
    }

    static void Main()
    {
        Base someInstance = new Derived();

        foreach (var m in someInstance.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance))
        {
            if (m.GetCustomAttribute(typeof(MyAttr)) != null)
            {
                m.Invoke(someInstance, null); // prints "foo"
            }
        }

        Console.ReadLine();
    }
}

You may change the null argument in the call to Invoke to the array of arguments you wish to pass to the function. The contents of that array must match the function signature.

This has nothing to do with WinForms. It's all about CLR runtime and its type system.

I don't know any way how you could "just do it".

You can check if a method M has an attribute A only if you have a MethodInfo object that describes that method ( methodinfo.GetCustomAttributes() )

You can get MethodInfo s in several ways, but the easiest and most obvious is to get the Type object and ask it about its methods ( type.GetMethod()/type.GetMethods() ).

You can get a Type object in several ways too. If you have any object at hand, you can call its GetType() method. Or, you can ask an Assembly object (that describes a DLL or EXE) about the Types it defines. (..)

So, if you have a foo object that someone already created:

call foo.GetType()
loop over type.GetMethods()
    call method.GetCustomAttributes(typeof(YourAttribute))
    check if it was found

Now if you notice that it as been found, you will end up having a MethodInfo that matches a method with that attribute. The only thing left is to call that method with methodinfo.Invoke and to pass it both parameters and the foo object .

Situation gets tricky if you don't have a foo object that you want to scan for methods. You must get the whole assembly, scan all types, scan all their methods. You end up with matching MethodInfo again. But you don't have any object to call the method found upon. Either that method will need to be static (so callable without target object) or you will need to somehow get matching object, too.

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