簡體   English   中英

Assembly.CreateInstance和Activator.CreateInstance之間的區別?

[英]Difference between Assembly.CreateInstance and Activator.CreateInstance?

這些電話有什么區別?

沒有。 Assembly.CreateInstance實際上調用了Activator.CreateInstance。

在Assembly.CreateInstance上使用Reflector:

public object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)
{
    Type type = this.GetType(typeName, false, ignoreCase);
    if (type == null)
    {
        return null;
    }
    return Activator.CreateInstance(type, bindingAttr, binder, args, culture, activationAttributes);
}

Assembly.CreateInstance在特定程序集中查找類型,而Activator.CreateInstance可以創建任何類型的對象。

Activator.CreateInstance具有Assembly沒有的重載; 例如,它可以使用遠程處理在其他應用程序域或其他服務器上創建對象。

您可以使用類型的名稱和程序集的名稱而不是Type對象來提供Activator.CreateInstance 這意味着它將嘗試將程序集加載到當前AppDomain(如果當前未加載),然后嘗試加載該類型。 我相信如果沒有加載Assembly.CreateInstance(它調用Activator)不會嘗試加載程序集。 它只是嘗試獲取指定類型的Type對象,如果找不到則返回null(我通過閱讀代碼而不是在測試之后這樣說)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM