首页 > Windows开发 > 详细

C# load and unload dll

时间:2021-05-25 22:28:45      阅读:18      评论:0      收藏:0      [点我收藏+]

1. Invoker

    Any c# project

    Create a new application domain

    Create a proxy within the domain

    Unload the application domain

AppDomain ad = AppDomain.CreateDomain("DLL Unload test");
ProxyObject obj = ProxyObject)ad.CreateInstanceFromAndUnwrap(@"UnloadDll.exe", "UnloadDll.ProxyObject");
obj.LoadAssembly();
obj.Invoke("TestDll.Class1", "Test", "It‘s a test");
AppDomain.Unload(ad);
obj = null;

 

2. Proxy

    Implement MarshalByRefObject

    Use reflection to load dll

    Dll is unloaded when the parent application domain unloading

class ProxyObject : MarshalByRefObject
{
    Assembly assembly = null;
    public void LoadAssembly()
    {
        assembly = Assembly.LoadFile(@"TestDLL.dll");           
    }
    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;           
    }
}

 

C# load and unload dll

原文:https://www.cnblogs.com/xyphoenix/p/14810108.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!