反射加载程序集

偶然想使用匹配文件名进行加载程序集

  public List<object> LoadAssembly(List<string> fileList, string ClassStart = "Start_", string MethodName = "Start")

        {

            List<object> objectList = new List<object>();

            {

                if (fileList != null && fileList.Count > 0)

                {

                    fileList.ForEach(paht =>

                    {

                        var type = Assembly.LoadFile(paht);

                        var methodList = from i in type.DefinedTypes where i.Name.ToString().StartsWith(ClassStart) select i;

                        methodList.ToList().ForEach(i =>

                        {

                            Object obj = i.Assembly.CreateInstance(i.FullName, true);

                            var mwthod = i.GetMethod(MethodName);

                            objectList.Add(mwthod.Invoke(obj, null));

                        });

                    });

                }

            }

            return objectList;

        }
View Code

 

你可能感兴趣的:(反射)