System.Activator.CreateInstanceFrom 方法 (AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

方法描述

使用命名的程序集文件和最匹配所指定参数的构造函数,来创建其名称在指定的远程域中指定的类型的实例。

语法定义(C# System.Activator.CreateInstanceFrom 方法 (AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[]) 的用法)

public static ObjectHandle CreateInstanceFrom(
	AppDomain domain,
	string assemblyFile,
	string typeName,
	bool ignoreCase,
	BindingFlags bindingAttr,
	Binder binder,
	Object[] args,
	CultureInfo culture,
	Object[] activationAttributes
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
domain System-AppDomain 在其中创建名为 typeName 的类型的远程域。
assemblyFile System-String 包含某程序集的文件的名称,将在该程序集内查找名为 typeName 的类型。
typeName System-String 首选类型的名称。
ignoreCase System-Boolean 使用 true 指定对 typeName 的搜索不区分大小写;使用 false 则指定搜索区分大小写。
bindingAttr System-Reflection-BindingFlags 影响 typeName 构造函数搜索的零个或多个位标志的组合。如果 bindingAttr 为零,则对公共构造函数进行区分大小写的搜索。
binder System-Reflection-Binder 使用 bindingAttr 和 args 来查找和标识 typeName 构造函数的对象。如果 binder 为 null,则使用默认联编程序。
args System-Object[] 与要调用构造函数的参数数量、顺序和类型匹配的参数数组。如果 args 为空数组或 null,则调用不带任何参数的构造函数(默认构造函数)。
culture System-Globalization-CultureInfo 区域性特定的信息,这些信息控制将 args 强制转换为 typeName 构造函数所声明的正式类型。如果 culture 为 null,则使用当前线程的 CultureInfo。
activationAttributes System-Object[] 包含一个或多个可以参与激活的特性的数组。通常为包含单个 UrlAttribute 对象的数组。UrlAttribute 指定激活远程对象所需的 URL。
返回值 System.Runtime.Remoting.ObjectHandle 要访问新创建的实例则必须解包的句柄。

提示和注释

当主机需要在具有受限安全权限的应用程序域中执行代码时,请使用 CreateInstanceFrom。

使用 ObjectHandle.Unwrap 方法解包返回值。

activationAttributes 参数与客户端激活对象相关;请参见Client Activation。

有关所调用方法可以引发的其他异常的信息,请参见 Assembly.LoadFrom 和 CreateInstance 方法的“异常”节。

System.Activator.CreateInstanceFrom 方法 (AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])例子


异常

异常 异常描述
ArgumentNullException domain 或 typeName 为 null。
MissingMethodException 未找到匹配的构造函数。
TypeLoadException 在 assemblyFile 中找不到 typename。
FileNotFoundException 未找到 assemblyFile。
MethodAccessException 调用方没有调用此构造函数的权限。
MemberAccessException 无法创建抽象类的实例,或者用后期绑定机制调用了该成员。
TargetInvocationException 通过反射调用的构造函数引发了异常。
SecurityException 调用方确实具有所需的 FileIOPermission。
NotSupportedException activationAttributes 不是空数组,并且正创建的类型未从 MarshalByRefObject 派生。
BadImageFormatException
  • assemblyFile 不是有效程序集。
  • assemblyName 是针对晚于当前加载版本的公共语言运行时版本编译的。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4 .NET Framework Client Profile 受以下版本支持:4

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。