能夠經過該方法來實現程序插件化。ide
假設A,B兩個類,A類爲宿主,B類爲插件須要加載到A類中:spa
class Program { public interface IHellow { void Hellow(); } static void Main(string[] args) { Assembly ass = Assembly.LoadFrom("D:/xx.dll"); var wormMain = ass.GetTypes().FirstOrDefault(m => m.GetInterface(typeof(IHellow).Name) != null); var tmpObj = (IHellow)Activator.CreateInstance(wormMain); tmpObj.Hellow(); Console.Read(); } }
namespace ClassLibrary1 { public class WormMain : IHellow { public void Hellow() { Console.WriteLine("Test Ok"); } } }
須要引用一下A類的項目文件,以獲得接口。插件
若是是連同依賴項一塊兒被載入,應該調用Assembly.LoadFromcode
A類輸出結果:orm
"Test Ok"