动态调用 , 卸载 Assembly
1.其实还是很麻烦的. 主要是使用起来麻烦, 你不能直接在本地调用,使用Remote Type .
2.初级资料: http://www.cnblogs.com/wayfarer/archive/2004/09/29/47896.html
3.完整的:
http://blog.csdn.net/westfruit/article/details/5501409
http://www.cnblogs.com/yuxuanji/articles/1583422.html
注意:
AppDomain 的安装是一个非常关键的部分。AssemblyProcessor 类使用 Assembly.Load() 定位程序集,它在 AppDomain 的应用程序库中寻找程序集。因此,我们需要将应用程序库设置为程序集所在的目录。这可以通过以下命令行来完成:
setup.ApplicationBase = fileInfo.DirectoryName;
还要注意86/64的问题
class RemoteLoader : MarshalByRefObject { public RemoteLoader() { AppDomain.CurrentDomain.AssemblyResolve+=CurrentDomain_AssemblyResolve; } public void SetDomain(AppDomain arg_domain) { arg_domain.AssemblyResolve += CurrentDomain_AssemblyResolve; } Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { MessageBox.Show(args.Name); return null; } public Assembly assembly = null; public string FullName { get { return assembly.FullName; } } public void TestTypes(Func<Type,bool> arg_fn) { var tps = assembly.GetTypes(); foreach (var tp in tps) { if (arg_fn(tp)) return; } } public void LoadAssembly(string arg_strAssemblyFullName) { assembly = Assembly.LoadFile(arg_strAssemblyFullName); } public bool Invoke(string fullClassName, string methodName, params Object[] args) { if (assembly == null) return false; Type tp = assembly.GetType(fullClassName); if (tp == null) return false; MethodInfo method = tp.GetMethod(methodName); if (method == null) return false; Object obj = Activator.CreateInstance(tp); method.Invoke(obj, args); return true; } public bool Compare() { var types = assembly.GetTypes(); foreach (var type in types) { if (type.IsIncludeType(typeof(DataProviderLibrary.DataProvider))) MessageBox.Show(type.ToString()); } return false; } } public class LocalLoader { private AppDomain appDomain; private RemoteLoader remoteLoader; public LocalLoader() { AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationName = "Test"; setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "private"); setup.CachePath = setup.ApplicationBase; setup.ShadowCopyFiles = "true"; setup.ShadowCopyDirectories = setup.ApplicationBase; appDomain = AppDomain.CreateDomain("TestDomain", null, setup); string name = Assembly.GetExecutingAssembly().GetName().FullName; remoteLoader = (RemoteLoader)appDomain.CreateInstanceAndUnwrap( name, typeof(RemoteLoader).FullName); remoteLoader.SetDomain(appDomain); } public void LoadAssembly(string fullName) { remoteLoader.LoadAssembly(fullName); } public void Unload() { AppDomain.Unload(appDomain); appDomain = null; } public string FullName { get { return remoteLoader.FullName; } } public bool Compare() { return remoteLoader.Compare(); } public void TestTypes(Func<Type,bool> arg_fn) { remoteLoader.TestTypes(arg_fn); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能