using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using Contracts;
namespace Test
{
class Program
{
static void Main(string[] args)
{
SetupPlugEnvironment();
ExecuteOperator("1.0.0.0");
ExecuteOperator("2.0.0.0");
}
private static void ExecuteOperator(string version)
{
var operatorType = Type.GetType("Implements.Operator, Implements, version = " + version + "");
var operatorInstance = Activator.CreateInstance(operatorType) as IOperator;
operatorInstance.Operate();
}
private static void SetupPlugEnvironment()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
AssemblyName name = new AssemblyName(args.Name);
var file = Path.Combine(
@"E:\Coding\HappyStudy\LoadContextStudy\Test\bin\Debug\Plugs",
name.Name,
name.Version.ToString(),
name.Name + ".dll");
Console.WriteLine("加載插件:" + name.Version);
return Assembly.LoadFile(file);
}
}
}
微軟不推薦使用 LoadFile(會(huì)加載相同標(biāo)識(shí)的程序集多次),Add In 采用的是 AppDomain,MEF 采用的是 LoadFrom。