Dynamically load a DLL from a specific folder?

后端 未结 5 785

At the moment, I have this code :

var shellViewLibrary = Assembly.LoadFrom(Path.Combine(_DllsPath, _DllShellView));
IEnumerable types = shellView         


        
5条回答
  •  萌比男神i
    2021-01-13 18:24

    This is an example of "Dynamically Loading a .dll from a Specific Folder" at runtime.

    // Check if user has access to requested .dll.
    string strDllPath = Path.GetFullPath(strSomePath);
    if (File.Exists(strDllPath))
    {
        // Execute the method from the requested .dll using reflection (System.Reflection).
        Assembly DLL = Assembly.LoadFrom(strDllPath);
        Type classType = DLL.GetType(String.Format("{0}.{1}", strNmSpaceNm, strClassNm));
        if (classType != null)
        {
            // Create class instance.
            classInst = Activator.CreateInstance(classType);
    
            // Invoke required method.
            MethodInfo methodInfo = classType.GetMethod(strMethodName);
            if (methodInfo != null)
            {
                object result = null;
                result = methodInfo.Invoke(classInst, new object[] { dllParams });
                return result.ToString();
            }
        }
    }
    

    This took me a while to work out so I hope it is of some use...

提交回复
热议问题