在c#中卸载外部类库?
可能重复:在c#中卸载外部类库?,c#,.net,dll,mef,C#,.net,Dll,Mef,可能重复: 在我的项目中,我使用MEF框架来提供可扩展性; 主程序可以由另一个Lib.dll类库扩展 问题是我需要在不关闭主程序的情况下,将这个Lib.dll与另一个Lib.dll进行交换 那么我如何卸载这个Lib.dll来交换它呢 更新 主程序的形式如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.Compo
在我的项目中,我使用MEF框架来提供可扩展性; 主程序可以由另一个Lib.dll类库扩展 问题是我需要在不关闭主程序的情况下,将这个Lib.dll与另一个Lib.dll进行交换 那么我如何卸载这个Lib.dll来交换它呢 更新 主程序的形式如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace SimpleCalculator3
{
public interface ICalculator
{
String Calculate(String input);
}
public interface IOperation
{
int Operate(int left, int right);
}
public interface IOperationData
{
Char Symbol { get; }
}
[Export(typeof(IOperation))]
[ExportMetadata("Symbol", '+')]
class Add : IOperation
{
public int Operate(int left, int right)
{
return left + right;
}
}
[Export(typeof(IOperation))]
[ExportMetadata("Symbol", '-')]
class Subtract : IOperation
{
public int Operate(int left, int right)
{
return left - right;
}
}
[Export(typeof(ICalculator))]
class MySimpleCalculator : ICalculator
{
[ImportMany]
IEnumerable<Lazy<IOperation, IOperationData>> operations;
public String Calculate(String input)
{
int left;
int right;
Char operation;
int fn = FindFirstNonDigit(input); //finds the operator
if (fn < 0) return "Could not parse command.";
try
{
//separate out the operands
left = int.Parse(input.Substring(0, fn));
right = int.Parse(input.Substring(fn + 1));
}
catch
{
return "Could not parse command.";
}
operation = input[fn];
foreach (Lazy<IOperation, IOperationData> i in operations)
{
if (i.Metadata.Symbol.Equals(operation)) return i.Value.Operate(left, right).ToString();
}
return "Operation Not Found!";
}
private int FindFirstNonDigit(String s)
{
for (int i = 0; i < s.Length; i++)
{
if (!(Char.IsDigit(s[i]))) return i;
}
return -1;
}
}
class Program
{
private CompositionContainer _container;
[Import(typeof(ICalculator))]
public ICalculator calculator;
private Program()
{
//An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the Program class
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
//catalog.Catalogs.Add(new DirectoryCatalog("C:\\Users\\SomeUser\\Documents\\Visual Studio 2010\\Projects\\SimpleCalculator3\\SimpleCalculator3\\Extensions"));
//Create the CompositionContainer with the parts in the catalog
_container = new CompositionContainer(catalog);
//Fill the imports of this object
try
{
this._container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
static void Main(string[] args)
{
Program p = new Program(); //Composition is performed in the constructor
String s;
Console.WriteLine("Enter Command:");
while (true)
{
s = Console.ReadLine();
Console.WriteLine(p.calculator.Calculate(s));
}
}
}
}
也就是说,我想动态更改Lib.dll中的代码,并在主程序运行时构建它。您可以在另一个AppDomain中加载文件,不需要时可以卸载它
AppDomainSetup ads = new AppDomainSetup();
ads.PrivateBinPath = Path.GetDirectoryName("C:\\some.dll");
AppDomain ad2 = AppDomain.CreateDomain("AD2", null, ads);
ProxyDomain proxy = (ProxyDomain)ad2.CreateInstanceAndUnwrap(typeof(ProxyDomain).Assembly.FullName, typeof(ProxyDomain).FullName);
bool ok = proxy.LoadDll("C:\\some.dll");
AppDomain.Unload(ad2);
public class ProxyDomain : MarshalByRefObject
{
public bool LoadDll(string assemblyPath)
{
Assembly myDLL = Assembly.LoadFile(assemblyPath);
//use your dll here
}
}
您可以在另一个AppDomain中加载文件,不需要时可以卸载
AppDomainSetup ads = new AppDomainSetup();
ads.PrivateBinPath = Path.GetDirectoryName("C:\\some.dll");
AppDomain ad2 = AppDomain.CreateDomain("AD2", null, ads);
ProxyDomain proxy = (ProxyDomain)ad2.CreateInstanceAndUnwrap(typeof(ProxyDomain).Assembly.FullName, typeof(ProxyDomain).FullName);
bool ok = proxy.LoadDll("C:\\some.dll");
AppDomain.Unload(ad2);
public class ProxyDomain : MarshalByRefObject
{
public bool LoadDll(string assemblyPath)
{
Assembly myDLL = Assembly.LoadFile(assemblyPath);
//use your dll here
}
}
你需要卸载加载它的appdomain。为什么要卸载它…再次加载它会很昂贵..我已经用我想要的代码示例更新了我的帖子..@Micah我如何将它与MEF一起使用(MEF在合成阶段在幕后加载dll)?@micah谢谢我阅读了这个主题并得到了我的答案:)你需要卸载加载它的appdomain。为什么要卸载它…再次加载它会很昂贵。。我已经用我想要的代码示例更新了我的帖子。@micah我如何将它与MEF一起使用(MEF在合成阶段在后台加载dll)?@micah谢谢我读了这个话题,得到了我的答案:)