csharp C#_Reflection_Plugin_Templete.cs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#_Reflection_Plugin_Templete.cs相关的知识,希望对你有一定的参考价值。

using System;
using System.IO;
using System.Reflection;

namespace ConsoleApplication1
{    
    class Program
    {
        static void Main(string[] args)
        {
            string pluginFoler = @".\plugin";
            if (Directory.Exists(pluginFoler) == true)
            {
                ProceesPlugin(pluginFoler);
            }
        }

        private static void ProceesPlugin(string rootPath)
        {
            foreach (string dll in Directory.EnumerateFiles(rootPath, "*.dll"))
            {
                Assembly plugin = Assembly.LoadFrom(dll);

                Type entryType = FindEntryType(plugin);

                if (entryType == null)
                    continue;

                object inst = Activator.CreateInstance(entryType);

                MethodInfo entryMethod = FindStartupMethod(entryType);
                if (entryMethod == null)
                    continue;

                entryMethod.Invoke(inst , null);
            }
        }

        private static Type FindEntryType(Assembly plugin)
        {
            foreach (Type t in plugin.GetTypes())
            {
                foreach (object obj in t.GetCustomAttributes(false))
                {
                    if (obj.GetType().Name.Equals("PluginAttribute"))
                    {
                        return t;
                    }           
                }
            }

            return null;
        }

        private static MethodInfo FindStartupMethod(Type entryType)
        {
            foreach (MethodInfo m in entryType.GetMethods())
            {
                foreach (object obj in m.GetCustomAttributes(false))
                {
                    return m;
                }
            }

            return null;
        }
    }
}

以上是关于csharp C#_Reflection_Plugin_Templete.cs的主要内容,如果未能解决你的问题,请参考以下文章

csharp C#_local_db_Create.cs

csharp C#_Make_Random_ListOrder.cs

csharp C#_mongo_datetime_attributing.cs

csharp C#_Read_Json_and_Convert.cs

csharp C#_Ajax_Responsing_In_WebForms.cs

csharp 以C#方式读取DDS图像。转换自“[il_dds.c](https://github.com/DentonW/DevIL/blob/master/DevIL/src-IL/src/il_