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

方法描述

创建在指定程序集文件中定义的指定类型的新实例。

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

[ObsoleteAttribute("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public ObjectHandle CreateInstanceFrom(
	string assemblyFile,
	string typeName,
	bool ignoreCase,
	BindingFlags bindingAttr,
	Binder binder,
	Object[] args,
	CultureInfo culture,
	Object[] activationAttributes,
	Evidence securityAttributes
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
assemblyFile System-String 文件的名称(包括路径),该文件包含定义所请求类型的程序集。该程序集是使用 LoadFrom 方法加载的。
typeName System-String Type.FullName 属性返回的所请求类型的完全限定名称,包含命名空间而不是程序集。
ignoreCase System-Boolean 一个布尔值,指示是否执行区分大小写的搜索。
bindingAttr System-Reflection-BindingFlags 影响 typeName 构造函数搜索的零个或多个位标志的组合。如果 bindingAttr 为零,则对公共构造函数进行区分大小写的搜索。
binder System-Reflection-Binder 一个对象,它启用绑定、对实参类型的强制、对成员的调用,以及通过反射对 MemberInfo 对象的检索。如果 binder 为 null,则使用默认联编程序。
args System-Object[] 要传递给构造函数的实参。此实参数组必须在数量、顺序和类型方面与要调用的构造函数的形参匹配。如果默认的构造函数是首选构造函数,则 args 必须为空数组或 Null。
culture System-Globalization-CultureInfo 区域性特定的信息,这些信息控制将 args 强制转换为 typeName 构造函数所声明的正式类型。如果 culture 为 null,则使用当前线程的 CultureInfo。
activationAttributes System-Object[] 包含一个或多个可以参与激活的特性的数组。通常是包含单个 UrlAttribute 对象的数组。UrlAttribute 指定激活远程对象所需的 URL。
securityAttributes System-Security-Policy-Evidence 用于授权创建 typeName 的信息。
返回值 System.Runtime.Remoting.ObjectHandle 一个对象,它是新实例的包装,或者如果找不到 typeName,则为 null。 需要打开包装才能访问真实对象。

提示和注释

有关此方法的更多信息,请参见 Activator.CreateInstanceFrom 方法。

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

如果使用 CreateInstanceFrom 方法在目标应用程序域(不是在其中执行调用操作的应用程序域)中创建实例,程序集将加载到目标应用程序域。 但是,如果该实例是在调用应用程序域中解包的,则以某些方式使用解包的实例会导致程序集加载到调用应用程序域中。 例如,对该实例进行解包后,可能会请求该实例的类型信息,以便调用其后期绑定方法。 将程序集加载到调用应用程序域时,会发生异常。

如果以前曾将同一程序集的另一版本加载到调用应用程序域,或者调用应用程序域的加载路径与目标应用程序域的加载路径不同,则会发生 MissingMethodException 这样的异常。

如果调用应用程序域对实例类型进行早期绑定调用,则尝试强制转换实例时会引发 InvalidCastException。

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


异常

异常 异常描述
ArgumentNullException
  • assemblyFile 为 null。
  • typeName 为 null。
NotSupportedException
  • 调用方无法为不是从 MarshalByRefObject 继承的对象提供激活特性。
  • securityAttributes 不是 null。 未启用旧版 CAS 策略时,securityAttributes 应该为 null。
AppDomainUnloadedException 尝试对已卸载的应用程序域进行操作。
FileNotFoundException 未找到 assemblyFile。
TypeLoadException 在 assemblyFile 中找不到 typeName。
MissingMethodException 找不到匹配的公共构造函数。
MethodAccessException 调用方的权限不足以调用此构造函数。
BadImageFormatException
  • assemblyFile 不是有效程序集。
  • 当前加载的是 2.0 或更高版本的公共语言运行时,assemblyFile 是用更高版本的公共语言运行时编译的。
FileLoadException 用两个不同的证据将一个程序集或模块加载了两次。
NullReferenceException 此实例为 null。

命名空间

namespace: System

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

版本信息

.NET Framework 受以下版本支持:3.5、3.0、2.0、1.1、1.0 在 4 中过时(编译器警告) .NET Framework Client Profile 受以下版本支持:3.5 SP1 在 4 中过时(编译器警告)

适用平台

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