Can I load a .NET assembly at runtime and instantiate a type knowing only the name?

后端 未结 13 2193
-上瘾入骨i
-上瘾入骨i 2020-11-22 17:21

Is it possible to instantiate an object at runtime if I only have the DLL name and the class name, without adding a reference to the assembly in the project? The class imple

相关标签:
13条回答
  • 2020-11-22 17:38

    Consider the limitations of the different Load* methods. From the MSDN docs...

    LoadFile does not load files into the LoadFrom context, and does not resolve dependencies using the load path, as the LoadFrom method does.

    More information on Load Contexts can be found in the LoadFrom docs.

    0 讨论(0)
  • 2020-11-22 17:39

    I found this question and some answers very useful, however I did have path problems, so this answer would cover loading library by finding bin directory path.

    First solution:

    string assemblyName = "library.dll";
    string assemblyPath = HttpContext.Current.Server.MapPath("~/bin/" + assemblyName);
    Assembly assembly = Assembly.LoadFrom(assemblyPath);
    Type T = assembly.GetType("Company.Project.Classname");
    Company.Project.Classname instance = (Company.Project.Classname) Activator.CreateInstance(T);
    

    Second solution

    string assemblyName = "library.dll";
    string assemblyPath = HttpContext.Current.Server.MapPath("~/bin/" + assemblyName);
    Assembly assembly = Assembly.LoadFile(assemblyPath);
    (Company.Project.Classname) instance = (Company.Project.Classname) assembly.CreateInstance("Company.Project.Classname");
    

    You can use same principle for interfaces (you would be creating a class but casting to interface), such as:

    (Company.Project.Interfacename) instance = (Company.Project.Interfacename) assembly.CreateInstance("Company.Project.Classname");
    

    This example is for web application but similar could be used for Desktop application, only path is resolved in different way, for example

    Path.GetDirectoryName(Application.ExecutablePath)
    
    0 讨论(0)
  • 2020-11-22 17:41

    It's Easy.

    Example from MSDN:

    public static void Main()
    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);
    }
    

    Here's a reference link

    https://msdn.microsoft.com/en-us/library/25y1ya39.aspx

    0 讨论(0)
  • 2020-11-22 17:44

    You can load an assembly using *Assembly.Load** methods. Using Activator.CreateInstance you can create new instances of the type you want. Keep in mind that you have to use the full type name of the class you want to load (for example Namespace.SubNamespace.ClassName). Using the method InvokeMember of the Type class you can invoke methods on the type.

    Also, take into account that once loaded, an assembly cannot be unloaded until the whole AppDomain is unloaded too (this is basically a memory leak).

    0 讨论(0)
  • 2020-11-22 17:45
    Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");
    
    Type type = assembly.GetType("MyType");
    
    dynamic instanceOfMyType = Activator.CreateInstance(type);
    

    So in this way you can use functions not with getting methodinfo,and then invoking it.You will do like this instanceOfMyType.MethodName(); But you can't use Intellisense because dynamic types are typed in runtime,not in compile time.

    0 讨论(0)
  • 2020-11-22 17:48

    Starting from Framework v4.5 you can use Activator.CreateInstanceFrom() to easily instantiate classes within assemblies. The following example shows how to use it and how to call a method passing parameters and getting return value.

        // Assuming moduleFileName contains full or valid relative path to assembly    
        var moduleInstance = Activator.CreateInstanceFrom(moduleFileName, "MyNamespace.MyClass");
        MethodInfo mi = moduleInstance.Unwrap().GetType().GetMethod("MyMethod");
        // Assuming the method returns a boolean and accepts a single string parameter
        bool rc = Convert.ToBoolean(mi.Invoke(moduleInstance.Unwrap(), new object[] { "MyParamValue" } ));
    
    0 讨论(0)
提交回复
热议问题