[英]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.