日期:2014-05-17 浏览次数:20961 次
public class Person { private string name; private int age; private string content; }
namespace ReflecLibrary2 { public class MainWindow { public MainWindow() { Welcome(); } private void Welcome() { Console.Write(@"当前程序为:ReflecLibrary2 "); Console.WriteLine(@"开始执行ReflecLibrary2!"); } } } namespace ReflectLibrary1 { public class MainWindow { public MainWindow() { Welcome(); } private void Welcome() { Console.Write(@"当前程序为:ReflectLibrary1 "); Console.WriteLine(@"开始执行ReflecLibrary1!"); } } }
class Program { static void Main(string[] args) { /////////////////////设置约定的规则,比如需要加载的程序的目录,程序集程序入口的类/////////////////// string startPath = AppDomain.CurrentDomain.BaseDirectory + @"Library\"; string suffix=@".dll"; string commonMainClass = @"MainWindow"; DirectoryInfo directory = new DirectoryInfo(startPath); /////////////////////将程序集文件名读入,这里其实只需要string类型的路径即可, //////为了后面处理字符串方便所以才读取文件信息 var libraries = directory.GetFiles().OrderBy(o=>o.FullName); List<FileInfo> loadDlls = new List<FileInfo>(); if (libraries != null) { foreach (FileInfo item in libraries) { if (item.FullName.ToLower().EndsWith(suffix)) { loadDlls.Add(item); } } } /////////////////////执行程序集/////////////////// //程序集1 Assembly assembly1 = Assembly.LoadFile(loadDlls[0].FullName.Replace(@"/", @"\")); string typeName1 = loadDlls[0].Name.Replace(loadDlls[0].Extension,string.Empty) + @"." + commonMainClass; assembly1.CreateInstance(typeName1); //程序集2 Assembly assembly2 = Assembly.LoadFile(loadDlls[1].FullName.Replace(@"/", @"\")); string typeName2 = loadDlls[1].Name.Replace(loadDlls[0].Extension, string.Empty) + @"." + commonMainClass; assembly2.CreateInstance(typeName2); Console.ReadLine(); }