2012-03-13 3 views
5

AppDomain과 Activator의 차이점을 알고 싶습니다. appdomain.CreateInstance를 통해 DLL을로드했습니다. 하지만 인스턴스를 만드는 더 많은 방법을 깨달았습니다. 따라서 언제 또는 어디에서이 방법을 선택합니까? 예 1 :AppDomain.CreateInstance와 Activator.CreateInstance의 차이점은 무엇입니까?

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

예 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; 
} 

예 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); 
    } 
} 

당신이 설명 할 수 있습니까?

답변

2

sscli2.0 소스 코드에서 Activator에 대한 호출을 항상 위임받은 AppDomain 클래스의 "CreateInstance"메서드 호출처럼 보입니다.

(거의 정적)의 유일한 목적 클래스는 "만들기"다양한 클래스의 인스턴스입니다 활성제, 응용 프로그램 도메인는 예를 들면, 목적 상당히 다른 (그리고 아마도 더 야심 찬) 도입 동안 :

  1. 경량 응용 단위 격리 단위.
  2. AppDomains를 언로드 할 수 있으므로 메모리 소비를 최적화하십시오.
  3. ...

제 1 회 및 제 3 예는 zmbq가 언급 한 것처럼 간단합니다. 귀하의 두 번째 예는 다음과 같습니다 post, 저자는 AppDomain을 사용하여 오래된 프록시를 언로드하는 방법을 보여주었습니다.

2

첫 번째 예제는 어셈블리 'example'에서 Example 유형의 인스턴스를 만들고 MethodA을 호출합니다.

는 세 번째는 내가, 내가 this이 무엇인지 모르는 두 번째에 대해 확실하지 않다 다른 AppDomain

MyType의 인스턴스를 생성하지만, 현재의 클래스를 만들 수 보인다 app-domain - 첫 번째 도메인과 유사합니다.