简体   繁体   English

Assembly.CreateInstance和Activator.CreateInstance之间的区别?

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

这些电话有什么区别?

None. 没有。 The Assembly.CreateInstance actually calls Activator.CreateInstance under the hood. Assembly.CreateInstance实际上调用了Activator.CreateInstance。

Using Reflector on Assembly.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 looks for a type in a particular assembly, whereas Activator.CreateInstance can create an object of any type. Assembly.CreateInstance在特定程序集中查找类型,而Activator.CreateInstance可以创建任何类型的对象。

Activator.CreateInstance has overloads that Assembly doesn't; Activator.CreateInstance具有Assembly没有的重载; for instance, it can create objects in other app domains, or on another server using Remoting. 例如,它可以使用远程处理在其他应用程序域或其他服务器上创建对象。

You can supply Activator.CreateInstance with the name of the type and the name of the assembly instead of a Type object. 您可以使用类型的名称和程序集的名称而不是Type对象来提供Activator.CreateInstance This means it will try to load the assembly into the current AppDomain (if not currently loaded) and then attempt to load the type. 这意味着它将尝试将程序集加载到当前AppDomain(如果当前未加载),然后尝试加载该类型。 I believe Assembly.CreateInstance (which does call Activator) does not attempt to load the assembly if it is not loaded. 我相信如果没有加载Assembly.CreateInstance(它调用Activator)不会尝试加载程序集。 It simply attempts to get the Type object for the specified type and returns null if it is not found (I say this by reading code and not after testing). 它只是尝试获取指定类型的Type对象,如果找不到则返回null(我通过阅读代码而不是在测试之后这样说)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Activator.CreateInstance和Activator.CreateInstance之间的区别<Type> - difference between Activator.CreateInstance and Activator.CreateInstance<Type> AppDomain.CreateInstance和Activator.CreateInstance有什么区别? - what is the difference between AppDomain.CreateInstance and Activator.CreateInstance? Activator.CreateInstance(string)和Activator.CreateInstance <T> () 区别 - Activator.CreateInstance(string) and Activator.CreateInstance<T>() difference Activator.CreateInstance和标准构造函数有什么区别? - What is the difference between Activator.CreateInstance and a standard constructor? Activator.CreateInstance与使用表达式之间是否存在明显差异? - Is there a noticible difference between Activator.CreateInstance and using expressions? Activator.CreateInstance的程序集名称是什么 - What would the assembly name be for Activator.CreateInstance Assembly.CreateInstance和安全性 - Assembly.CreateInstance and security Activator.CreateInstance()和typeof(T).InvokeMember()与BindingFlags.CreateInstance之间的区别 - Difference between Activator.CreateInstance() and typeof(T).InvokeMember() with BindingFlags.CreateInstance Activator.CreateInstance MissingMethodException - Activator.CreateInstance MissingMethodException Activator.CreateInstance() - Activator.CreateInstance()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM