Dynamically load a DLL from a specific folder?

后端 未结 5 748

At the moment, I have this code :

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


        
相关标签:
5条回答
  • 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...

    0 讨论(0)
  • 2021-01-13 18:28

    Once you call this line

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

    The assembly has been loaded in to memory. So long as you specify types correctly from this then you will be able to use Activator.CreateInstance to create the types. ie: It is not necessary to further specify where the type is.

    Regarding Activator, from MSDN the CreateInstance method can accept a System.Type. I would just use this method inside your if-statement:

    Activator.CreateInstance(Type type);
    

    What I would try to do to debug this is first create the type and then pass it in to CreateInstance. You may find that the Type creation itself is failing (due to unresolved assembly) or the instantiation of that type (due to exception in the constructor). At first glance your code here appears to be correct:

    foreach (Type type in types)      
    {          
        var typeIShellViewInterface = type.GetInterface(_NamespaceIShellView, false);          
        if (typeIShellViewInterface != null)          
        {              
            try
            {
                // I assume you are calling this line at the point marked 'here'. 
                // To debug the creation wrap in a try-catch and view the inner exceptions
                var result = Activator.CreateInstance(type);          
            }
            catch(Exception caught)
            {
                // When you hit this line, look at caught inner exceptions
                // I suspect you have a broken Xaml file inside WPF usercontrol
                // or Xaml resource dictionary used by type
                Debugger.Break();
            }
        }      
    }  
    

    In your question you specify that you are getting a XamlParseException. It sounds to me like the type in question is a UserControl (or otherwise refers to a WPF Xaml resource file) and there is an error in that Xaml file, i.e. nothing to do with your usage of Assembly.Load or Activator.CreateInstance.

    Could you try posting the inner exception(s) to get a better idea on what the problem is?

    0 讨论(0)
  • 2021-01-13 18:32

    I'm not sure what you mean by this:

    create an object whose type is type in a specific folder (that is outside the build folder)

    A type only exists in an assembly. Path is entirely irrelevant to types in .NET

    I assume that by "path" you really mean "namespace", meaning that you don't know in which namespace the type exists. You have already loaded the assembly in your code. Inspect the assembly using reflection to find the type you're looking for. Then pass the Type object that represents the type you're looking for to Activator.CreateInstance

    0 讨论(0)
  • 2021-01-13 18:37

    if you're trying to create a type with the Activator class from a DLL that's outside your application, you need to first load this DLL inside your application domain. The easiest and quickest way to do this is by using the Assembly.LoadFile method.

    more information about that method can be found here : http://msdn.microsoft.com/en-us/library/system.reflection.assembly.loadfile.aspx

    When the assembly is properly loaded you can use the Activator to create an instance from the type inside the DLL. We use this mechanic to run custom code in our application.

    0 讨论(0)
  • 2021-01-13 18:40

    Check out MEF and Prism. MEF is a dependency injection library that helps with this. You can load all of your dependencies from a specific folder and make dynamically load them.

    Prism is a pattern that leverages dependency injection and works great with MEF to load libraries dynamically

    0 讨论(0)
提交回复
热议问题