如何获取MEF中的所有方法

How to get all methods in MEF

我有属性class

[AttributeUsage(AttributeTargets.Method)]
public class MethodGetterAttribute : ExportAttribute
{

}

我在几个命名空间的方法中使用它:

namespace Model.First
{
    public class PersonBL
    {
        [MethodGetter]
        public void GetName(Person person)
        {

        }
    }
}

namespace Model.First.Second
{
    public class PersonBL
    {
        [MethodGetter]
        public void GetName(Person person)
        {

        }
    }
}

namespace Model.First.Second.Third
{
    public class WorkerBL
    {
        [MethodGetter]
        public void GetName(Worker worker)
        {

        }
    }
}

我想对所有方法进行排序,然后 运行 一个一个地排序。要获取方法,我这样做:

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(assemblies.FirstOrDefault(a => a.GetName().Name.Contains("Model"))));
var container = new CompositionContainer(catalog);
var importedMethods = container.GetExports<Action<Worker>>() as IEnumerable<Lazy<Action<Worker>>>;
var result = importedMethods.Select(a => a.Value.Target).ToList();// Here i'm getting only worker's method

但它returns只是Worker的方法。如何从 worker 获取所有三种方法?

嗯... 让我们创建 4 class 个库

Zero.dll 以及其他程序集中使用的所有 classes

using System;
using System.ComponentModel.Composition;
using System.Diagnostics;

namespace Zero
{
    [AttributeUsage(AttributeTargets.Method)]
    public class MethodGetterAttribute : ExportAttribute { }

    public class Person { }

    public class Worker : Person { }

    public static class MethodHelper
    {
        public static string GetMethod()
        {
            var method = new StackTrace().GetFrame(1).GetMethod();
            return $"{method.DeclaringType.FullName} {method}";
        }
    }

    public static class Discovery
    {
        public static TDelegate[] GetDelegates<TAttribure, TDelegate>()
            where TAttribure : Attribute
            where TDelegate : Delegate
        {
            return Directory.GetFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "*.dll")
                            .Select(file => { try { return Assembly.LoadFrom(file); } catch { return null; } })
                            .OfType<Assembly>()
                            .Append(Assembly.GetEntryAssembly())
                            .SelectMany(assembly => assembly.GetTypes())
                            .SelectMany(type => type.GetMethods())
                            .Where(method => method.GetCustomAttributes(typeof(TAttribure)).Any())
                            .Select(method => Delegate.CreateDelegate(typeof(TDelegate), null, method, false))
                            .OfType<TDelegate>()
                            .ToArray();
        }
    }
}

Model.First.dll 引用 Zero.dll

using System;
using Zero;

namespace Model.First
{
    public class PersonBL
    {
        [MethodGetter]
        public void GetName(Person person)
        {
            Console.WriteLine(MethodHelper.GetMethod());
        }
    }
}

Model.First.Second.dll 引用 Zero.dll

using System;
using Zero;

namespace Model.First.Second
{
    public class PersonBL
    {
        [MethodGetter]
        public void GetName(Person person)
        {
            Console.WriteLine(MethodHelper.GetMethod());
        }

        [MethodGetter]
        public void Incompatible(string s)
        {
            Console.WriteLine(MethodHelper.GetMethod());
        }
    }
}

Model.First.Second.Third.dll 引用 Zero.dll

using System;
using Zero;

namespace Model.First.Second.Third
{
    public class WorkerBL
    {
        [MethodGetter]
        public void GetName(Worker worker)
        {
            Console.WriteLine(MethodHelper.GetMethod());
        }

        public void NoAttribute(Worker worker)
        {
            Console.WriteLine(MethodHelper.GetMethod());
        }
    }
}

然后让我们创建控制台应用程序 ConsoleApp.exe 引用 Zero.dll、Model.First.dll、Model.First.Second.dll 和 Model.First.Second.Third.dll

using System;
using Zero;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var worker = new Worker();
            foreach (var d in Discovery.GetDelegates<MethodGetterAttribute, Action<Worker>>())
                d.Invoke(worker);
        }        
    }

    public class WorkerBL
    {
        [MethodGetter]
        public void GetName(Worker worker)
        {
            Console.WriteLine(MethodHelper.GetMethod());
        }
    }
}

让我们创建Junk.txt,把一些像bd%E56#EVwD这样的废话放进去,将文件重命名为Junk.dll并添加到.exe文件目录中,然后启动应用程序。

输出为:

Model.First.PersonBL Void GetName(Zero.Person)
Model.First.Second.PersonBL Void GetName(Zero.Person)
Model.First.Second.Third.WorkerBL Void GetName(Zero.Worker)
ConsoleApp.WorkerBL Void GetName(Zero.Worker)

不出所料。它会找到所有具有指定属性的兼容方法,并为它们找到 returns 个委托。