APPDomain加载卸载动态库

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C#代码示例,实现了动态加载卸载类的功能: ```csharp using System; using System.Reflection; public class DynamicClassLoader { private Assembly assembly; private Type type; public void LoadClass(string className, string assemblyName) { try { // Load the assembly assembly = Assembly.Load(assemblyName); // Get the type of the class type = assembly.GetType(className); Console.WriteLine("Class {0} loaded successfully.", className); } catch (Exception ex) { Console.WriteLine("Failed to load class {0}: {1}", className, ex.Message); } } public void UnloadClass() { try { // Unload the assembly AppDomain.CurrentDomain.GetAssemblies() .Where(a => a == assembly) .ToList() .ForEach(a => AppDomain.CurrentDomain.Load(a.GetName()).GetTypes()); Console.WriteLine("Class unloaded successfully."); } catch (Exception ex) { Console.WriteLine("Failed to unload class: {0}", ex.Message); } } public object CreateInstance() { try { // Create an instance of the class return Activator.CreateInstance(type); } catch (Exception ex) { Console.WriteLine("Failed to create instance of class {0}: {1}", type.FullName, ex.Message); return null; } } public void InvokeMethod(string methodName, object instance, params object[] parameters) { try { // Invoke the method on the instance MethodInfo method = type.GetMethod(methodName); method.Invoke(instance, parameters); } catch (Exception ex) { Console.WriteLine("Failed to invoke method {0}: {1}", methodName, ex.Message); } } } ``` 使用示例: ```csharp DynamicClassLoader loader = new DynamicClassLoader(); loader.LoadClass("MyClass", "MyAssembly"); object instance = loader.CreateInstance(); loader.InvokeMethod("MyMethod", instance); loader.UnloadClass(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值