What is the difference between AppDomain.CreateInstance and Activator.CreateInstance?

I want to ask a question to understand the difference between AppDomain and Activator, I loaded my DLL through appdomain.CreateInstance. But I realized that there are more methods for creating an instance. So when or where do I choose this method? Example 1:

    // 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);

Example 2:

public WsdlClassParser CreateWsdlClassParser()
{
    this.CreateAppDomain(null);

    string AssemblyPath = Assembly.GetExecutingAssembly().Location; 
    WsdlClassParser parser = null;
    try
    {                
        parser = (WsdlClassParser) this.LocalAppDomain.CreateInstanceFrom(AssemblyPath,
                                          typeof(Westwind.WebServices.WsdlClassParser).FullName).Unwrap() ;                
    }
    catch (Exception ex)
    {
        this.ErrorMessage = ex.Message;
    }                        
    return parser;
}

Example 3:

private static void InstantiateMyTypeSucceed(AppDomain domain)
{
    try
    {
        string asmname = Assembly.GetCallingAssembly().FullName;
        domain.CreateInstance(asmname, "MyType");
    }
    catch (Exception e)
    {
        Console.WriteLine();
        Console.WriteLine(e.Message);
    }
}

Can you explain why I need more methods or what are the differences?

+5
source share
2 answers

From the sscli2.0 source code, it looks like this: the "CreateInstance" method calls the AppDomain class always delegates the Activator call .

( ) Activator "" , AppDomain - (, , )), :

  • ;
  • , AppDomains .
  • ...

1- 3- , zmbq. , post, , out- AppDomain.

+2

Example ' MethodA.

MyType AppDomain

, , this, , , , .

+2

All Articles