AppDomain.CreateInstance 方法

定义

创建在指定程序集中定义的指定类型的新实例。Creates a new instance of a specified type defined in a specified assembly.

重载

CreateInstance(String, String)

创建在指定程序集中定义的指定类型的新实例。Creates a new instance of the specified type defined in the specified assembly.

CreateInstance(String, String, Object[])

创建在指定程序集中定义的指定类型的新实例。Creates a new instance of the specified type defined in the specified assembly. 形参指定激活特性数组。A parameter specifies an array of activation attributes.

CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

创建在指定程序集中定义的指定类型的新实例。Creates a new instance of the specified type defined in the specified assembly. 形参指定联编程序、绑定标志、构造函数实参、用于解释实参的特定于区域性的信息,以及可选激活特性。Parameters specify a binder, binding flags, constructor arguments, culture-specific information used to interpret arguments, and optional activation attributes.

CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)

创建在指定程序集中定义的指定类型的新实例。Creates a new instance of the specified type defined in the specified assembly. 参数指定联编程序、绑定标志、构造函数自变量、特定于区域性的信息,这些信息用于解释自变量、激活特性和授权,以创建类型。Parameters specify a binder, binding flags, constructor arguments, culture-specific information used to interpret arguments, activation attributes, and authorization to create the type.

CreateInstance(String, String)

创建在指定程序集中定义的指定类型的新实例。Creates a new instance of the specified type defined in the specified assembly.

public:
 virtual System::Runtime::Remoting::ObjectHandle ^ CreateInstance(System::String ^ assemblyName, System::String ^ typeName);
public System.Runtime.Remoting.ObjectHandle CreateInstance (string assemblyName, string typeName);
abstract member CreateInstance : string * string -> System.Runtime.Remoting.ObjectHandle
override this.CreateInstance : string * string -> System.Runtime.Remoting.ObjectHandle
Public Function CreateInstance (assemblyName As String, typeName As String) As ObjectHandle

参数

assemblyName
String

程序集的显示名称。The display name of the assembly. 请参阅FullNameSee FullName.

typeName
String

FullName 属性返回的所请求类型的完全限定名称,包含命名空间而不是程序集。The fully qualified name of the requested type, including the namespace but not the assembly, as returned by the FullName property.

返回

一个对象,该对象是 typeName 指定的新实例的包装。An object that is a wrapper for the new instance specified by typeName. 返回值需要打开包装才能访问真实对象。The return value needs to be unwrapped to access the real object.

实现

异常

在卸载的应用程序域上尝试该操作。The operation is attempted on an unloaded application domain.

assemblyNametypeNamenullassemblyName or typeName is null.

assemblyName 不是有效的程序集。assemblyName is not a valid assembly.

- 或 --or- 当前加载的是公共语言运行时 2.0 版或更高版本,而 assemblyName 是用更高版本编译的。Version 2.0 or later of the common language runtime is currently loaded and assemblyName was compiled with a later version.

一个程序集或模块用两个不同的证据加载了两次。An assembly or module was loaded twice with two different evidences.

未找到 assemblyNameassemblyName was not found.

调用方没有权限调用此构造函数。The caller does not have permission to call this constructor.

未找到匹配的公共构造函数。No matching public constructor was found.

typename 中未找到 assemblyNametypename was not found in assemblyName.

此示例为 nullThis instance is null.

注解

此方法为 typeName调用无参数的构造函数。This method calls the parameterless constructor for typeName.

请参阅 assemblyName格式 AssemblyNameSee AssemblyName for the format of assemblyName.

尝试对不是当前应用程序域的目标应用程序域调用 CreateInstance 将导致成功地在目标应用程序域中加载程序集。An attempt to call CreateInstance on a target application domain that is not the current application domain will result in a successful load of the assembly in the target application domain. 由于未 MarshalByRefObjectAssembly,因此当此方法尝试将已加载程序集的 Assembly 返回到当前应用程序域时,公共语言运行时将尝试将程序集加载到当前应用程序域中,并且加载可能会失败。Since an Assembly is not MarshalByRefObject, when this method attempts to return the Assembly for the loaded assembly to the current application domain, the common language runtime will try to load the assembly into the current application domain and the load might fail. 如果两个应用程序域的路径设置不同,则加载到当前应用程序域中的程序集可能不同于首先加载的程序集。The assembly that is loaded into the current application domain might be different from the assembly that was loaded first if the path settings for the two application domains are different.

安全性

FileIOPermission
用于从文件或目录读取的访问权限,以及用于访问路径本身中的信息的。for access to read from a file or directory, and for access to the information in the path itself. 关联的枚举: ReadPathDiscoveryAssociated enumerations: Read, PathDiscovery.

WebPermission
如果程序集不是本地的,则能够访问程序集的位置。for the ability to access the location of the assembly if the assembly is not local.

SecurityPermission
用于在创建委托的实例时调用非托管代码的能力。for the ability to call unmanaged code when creating an instance of a delegate. 关联的枚举: UnmanagedCodeAssociated enumeration: UnmanagedCode

另请参阅

CreateInstance(String, String, Object[])

创建在指定程序集中定义的指定类型的新实例。Creates a new instance of the specified type defined in the specified assembly. 形参指定激活特性数组。A parameter specifies an array of activation attributes.

public:
 virtual System::Runtime::Remoting::ObjectHandle ^ CreateInstance(System::String ^ assemblyName, System::String ^ typeName, cli::array <System::Object ^> ^ activationAttributes);
public System.Runtime.Remoting.ObjectHandle CreateInstance (string assemblyName, string typeName, object[] activationAttributes);
abstract member CreateInstance : string * string * obj[] -> System.Runtime.Remoting.ObjectHandle
override this.CreateInstance : string * string * obj[] -> System.Runtime.Remoting.ObjectHandle
Public Function CreateInstance (assemblyName As String, typeName As String, activationAttributes As Object()) As ObjectHandle

参数

assemblyName
String

程序集的显示名称。The display name of the assembly. 请参阅FullNameSee FullName.

typeName
String

FullName 属性返回的所请求类型的完全限定名称,包含命名空间而不是程序集。The fully qualified name of the requested type, including the namespace but not the assembly, as returned by the FullName property.

activationAttributes
Object[]

可参与激活的一个或多个属性的数组。An array of one or more attributes that can participate in activation. 通常,为包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。Typically, an array that contains a single UrlAttribute object that specifies the URL that is required to activate a remote object.

此参数与客户端激活的对象相关。客户端激活是一项传统技术,保留用于向后兼容,但不建议用于新的开发。This parameter is related to client-activated objects.Client activation is a legacy technology that is retained for backward compatibility but is not recommended for new development. 应改用 Windows Communication Foundation 来开发分布式应用程序。Distributed applications should instead use Windows Communication Foundation.

返回

一个对象,该对象是 typeName 指定的新实例的包装。An object that is a wrapper for the new instance specified by typeName. 返回值需要打开包装才能访问真实对象。The return value needs to be unwrapped to access the real object.

实现

异常

在卸载的应用程序域上尝试该操作。The operation is attempted on an unloaded application domain.

assemblyNametypeNamenullassemblyName or typeName is null.

assemblyName 不是有效的程序集。assemblyName is not a valid assembly.

- 或 --or- 当前加载的是公共语言运行时 2.0 版或更高版本,而 assemblyName 是用更高版本编译的。Version 2.0 or later of the common language runtime is currently loaded and assemblyName was compiled with a later version.

一个程序集或模块用两个不同的证据加载了两次。An assembly or module was loaded twice with two different evidences.

未找到 assemblyNameassemblyName was not found.

调用方没有权限调用此构造函数。The caller does not have permission to call this constructor.

未找到匹配的公共构造函数。No matching public constructor was found.

调用方不能为非继承自 MarshalByRefObject 的对象提供激活属性。The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.

typename 中未找到 assemblyNametypename was not found in assemblyName.

此示例为 nullThis instance is null.

注解

此方法为 typeName调用无参数的构造函数。This method calls the parameterless constructor for typeName.

请参阅 assemblyName格式 AssemblyNameSee AssemblyName for the format of assemblyName.

尝试对不是当前应用程序域的目标应用程序域调用 CreateInstance 将导致成功地在目标应用程序域中加载程序集。An attempt to call CreateInstance on a target application domain that is not the current application domain will result in a successful load of the assembly in the target application domain. 由于未 MarshalByRefObjectAssembly,因此当此方法尝试将已加载程序集的 Assembly 返回到当前应用程序域时,公共语言运行时将尝试将程序集加载到当前应用程序域中,并且加载可能会失败。Since an Assembly is not MarshalByRefObject, when this method attempts to return the Assembly for the loaded assembly to the current application domain, the common language runtime will try to load the assembly into the current application domain and the load might fail. 如果两个应用程序域的路径设置不同,则加载到当前应用程序域中的程序集可能不同于首先加载的程序集。The assembly that is loaded into the current application domain might be different from the assembly that was loaded first if the path settings for the two application domains are different.

安全性

FileIOPermissionAccess
用于读取包含程序集清单的文件。for the ability to read the file containing the assembly manifest. 关联的枚举: ReadAssociated enumeration: Read

WebPermission
如果程序集不是本地的,则能够访问程序集的位置。for the ability to access the location of the assembly if the assembly is not local.

SecurityPermission
用于在创建委托的实例时调用非托管代码的能力。for the ability to call unmanaged code when creating an instance of a delegate. 关联的枚举: UnmanagedCodeAssociated enumeration: UnmanagedCode

ReflectionPermission
用于在所有类型成员上调用操作的能力。for the ability to invoke operations on all type members. 关联的枚举: MemberAccessAssociated enumeration: MemberAccess

另请参阅

CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

创建在指定程序集中定义的指定类型的新实例。Creates a new instance of the specified type defined in the specified assembly. 形参指定联编程序、绑定标志、构造函数实参、用于解释实参的特定于区域性的信息,以及可选激活特性。Parameters specify a binder, binding flags, constructor arguments, culture-specific information used to interpret arguments, and optional activation attributes.

public:
 System::Runtime::Remoting::ObjectHandle ^ CreateInstance(System::String ^ assemblyName, System::String ^ typeName, bool ignoreCase, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ args, System::Globalization::CultureInfo ^ culture, cli::array <System::Object ^> ^ activationAttributes);
public System.Runtime.Remoting.ObjectHandle CreateInstance (string assemblyName, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes);
member this.CreateInstance : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> System.Runtime.Remoting.ObjectHandle

参数

assemblyName
String

程序集的显示名称。The display name of the assembly. 请参阅FullNameSee FullName.

typeName
String

FullName 属性返回的所请求类型的完全限定名称,包含命名空间而不是程序集。The fully qualified name of the requested type, including the namespace but not the assembly, as returned by the FullName property.

ignoreCase
Boolean

一个布尔值,指示是否执行区分大小写的搜索。A Boolean value specifying whether to perform a case-sensitive search or not.

bindingAttr
BindingFlags

影响 typeName 构造函数搜索的零个或多个位标志的组合。A combination of zero or more bit flags that affect the search for the typeName constructor. 如果 bindingAttr 为零,则对公共构造函数进行区分大小写的搜索。If bindingAttr is zero, a case-sensitive search for public constructors is conducted.

binder
Binder

一个对象,它使用反射启用绑定、参数类型的强制、成员的调用和 MemberInfo 对象的检索。An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects using reflection. 如果 binder 为 null,则使用默认联编程序。If binder is null, the default binder is used.

args
Object[]

要传递给构造函数的实参。The arguments to pass to the constructor. 此实参数组必须在数量、顺序和类型方面与要调用的构造函数的形参匹配。This array of arguments must match in number, order, and type the parameters of the constructor to invoke. 如果无参数构造函数是首选,则 args 必须为空数组或 NULL。If the parameterless constructor is preferred, args must be an empty array or null.

culture
CultureInfo

区域性特定的信息,这些信息控制将 args 强制转换为 typeName 构造函数所声明的正式类型。Culture-specific information that governs the coercion of args to the formal types declared for the typeName constructor. 如果 culturenull,则使用当前线程的 CultureInfoIf culture is null, the CultureInfo for the current thread is used.

activationAttributes
Object[]

可参与激活的一个或多个属性的数组。An array of one or more attributes that can participate in activation. 通常,为包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。Typically, an array that contains a single UrlAttribute object that specifies the URL that is required to activate a remote object.

此参数与客户端激活的对象相关。This parameter is related to client-activated objects. 客户端激活是一项传统技术,保留用于向后兼容,但不建议用于新的开发。Client activation is a legacy technology that is retained for backward compatibility but is not recommended for new development. 应改用 Windows Communication Foundation 来开发分布式应用程序。Distributed applications should instead use Windows Communication Foundation.

返回

一个对象,该对象是 typeName 指定的新实例的包装。An object that is a wrapper for the new instance specified by typeName. 返回值需要打开包装才能访问真实对象。The return value needs to be unwrapped to access the real object.

异常

在卸载的应用程序域上尝试该操作。The operation is attempted on an unloaded application domain.

assemblyNametypeNamenullassemblyName or typeName is null.

assemblyName 不是有效的程序集。assemblyName is not a valid assembly.

- 或 --or- assemblyName 使用高于当前所加载版本的公共语言运行时版本编译而成。assemblyName was compiled with a later version of the common language runtime than the version that is currently loaded.

一个程序集或模块用两个不同的证据加载了两次。An assembly or module was loaded twice with two different evidences.

未找到 assemblyNameassemblyName was not found.

调用方没有权限调用此构造函数。The caller does not have permission to call this constructor.

未找到匹配的构造函数。No matching constructor was found.

调用方不能为非继承自 MarshalByRefObject 的对象提供激活属性。The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.

typename 中未找到 assemblyNametypename was not found in assemblyName.

此示例为 nullThis instance is null.

注解

请参阅 assemblyName格式 AssemblyNameSee AssemblyName for the format of assemblyName.

尝试对不是当前应用程序域的目标应用程序域调用 CreateInstance 将导致成功地在目标应用程序域中加载程序集。An attempt to call CreateInstance on a target application domain that is not the current application domain will result in a successful load of the assembly in the target application domain. 由于未 MarshalByRefObjectAssembly,因此当此方法尝试将已加载程序集的 Assembly 返回到当前应用程序域时,公共语言运行时将尝试将程序集加载到当前应用程序域中,并且加载可能会失败。Since an Assembly is not MarshalByRefObject, when this method attempts to return the Assembly for the loaded assembly to the current application domain, the common language runtime will try to load the assembly into the current application domain and the load might fail. 如果两个应用程序域的路径设置不同,则加载到当前应用程序域中的程序集可能不同于首先加载的程序集。The assembly that is loaded into the current application domain might be different from the assembly that was loaded first if the path settings for the two application domains are different.

安全性

FileIOPermissionAccess
用于读取包含程序集清单的文件。for the ability to read the file containing the assembly manifest. 关联的枚举: ReadAssociated enumeration: Read

WebPermission
如果程序集不是本地的,则能够访问程序集的位置。for the ability to access the location of the assembly if the assembly is not local.

SecurityPermission
用于在创建委托的实例时调用非托管代码的能力。for the ability to call unmanaged code when creating an instance of a delegate. 关联的枚举: UnmanagedCodeAssociated enumeration: UnmanagedCode

ReflectionPermission
用于在所有类型成员上调用操作的能力。for the ability to invoke operations on all type members. 关联的枚举: MemberAccessAssociated enumeration: MemberAccess

另请参阅

CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)

警告

此 API 现已过时。

创建在指定程序集中定义的指定类型的新实例。Creates a new instance of the specified type defined in the specified assembly. 参数指定联编程序、绑定标志、构造函数自变量、特定于区域性的信息,这些信息用于解释自变量、激活特性和授权,以创建类型。Parameters specify a binder, binding flags, constructor arguments, culture-specific information used to interpret arguments, activation attributes, and authorization to create the type.

public:
 System::Runtime::Remoting::ObjectHandle ^ CreateInstance(System::String ^ assemblyName, System::String ^ typeName, bool ignoreCase, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ args, System::Globalization::CultureInfo ^ culture, cli::array <System::Object ^> ^ activationAttributes, System::Security::Policy::Evidence ^ securityAttributes);
[System.Obsolete("Use an overload that does not take an Evidence parameter")]
[System.Obsolete("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 CreateInstance which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Runtime.Remoting.ObjectHandle CreateInstance (string assemblyName, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes, System.Security.Policy.Evidence securityAttributes);
member this.CreateInstance : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle

参数

assemblyName
String

程序集的显示名称。The display name of the assembly. 请参阅FullNameSee FullName.

typeName
String

FullName 属性返回的所请求类型的完全限定名称,包含命名空间而不是程序集。The fully qualified name of the requested type, including the namespace but not the assembly, as returned by the FullName property.

ignoreCase
Boolean

一个布尔值,指示是否执行区分大小写的搜索。A Boolean value specifying whether to perform a case-sensitive search or not.

bindingAttr
BindingFlags

影响 typeName 构造函数搜索的零个或多个位标志的组合。A combination of zero or more bit flags that affect the search for the typeName constructor. 如果 bindingAttr 为零,则对公共构造函数进行区分大小写的搜索。If bindingAttr is zero, a case-sensitive search for public constructors is conducted.

binder
Binder

一个对象,它使用反射启用绑定、参数类型的强制、成员的调用和 MemberInfo 对象的检索。An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects using reflection. 如果 binder 为 null,则使用默认联编程序。If binder is null, the default binder is used.

args
Object[]

要传递给构造函数的实参。The arguments to pass to the constructor. 此实参数组必须在数量、顺序和类型方面与要调用的构造函数的形参匹配。This array of arguments must match in number, order, and type the parameters of the constructor to invoke. 如果无参数构造函数是首选,则 args 必须为空数组或 NULL。If the parameterless constructor is preferred, args must be an empty array or null.

culture
CultureInfo

区域性特定的信息,这些信息控制将 args 强制转换为 typeName 构造函数所声明的正式类型。Culture-specific information that governs the coercion of args to the formal types declared for the typeName constructor. 如果 culturenull,则使用当前线程的 CultureInfoIf culture is null, the CultureInfo for the current thread is used.

activationAttributes
Object[]

可参与激活的一个或多个属性的数组。An array of one or more attributes that can participate in activation. 通常,为包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。Typically, an array that contains a single UrlAttribute object that specifies the URL that is required to activate a remote object.

此参数与客户端激活的对象相关。客户端激活是一项传统技术,保留用于向后兼容,但不建议用于新的开发。This parameter is related to client-activated objects.Client activation is a legacy technology that is retained for backward compatibility but is not recommended for new development. 应改用 Windows Communication Foundation 来开发分布式应用程序。Distributed applications should instead use Windows Communication Foundation.

securityAttributes
Evidence

用于授权创建 typeName 的信息。Information used to authorize creation of typeName.

返回

一个对象,该对象是 typeName 指定的新实例的包装。An object that is a wrapper for the new instance specified by typeName. 返回值需要打开包装才能访问真实对象。The return value needs to be unwrapped to access the real object.

实现

属性

异常

在卸载的应用程序域上尝试该操作。The operation is attempted on an unloaded application domain.

assemblyNametypeNamenullassemblyName or typeName is null.

assemblyName 不是有效的程序集。assemblyName is not a valid assembly.

- 或 --or- 当前加载的是公共语言运行时 2.0 版或更高版本,而 assemblyName 是用更高版本编译的。Version 2.0 or later of the common language runtime is currently loaded and assemblyName was compiled with a later version.

一个程序集或模块用两个不同的证据加载了两次。An assembly or module was loaded twice with two different evidences.

未找到 assemblyNameassemblyName was not found.

调用方没有权限调用此构造函数。The caller does not have permission to call this constructor.

未找到匹配的构造函数。No matching constructor was found.

调用方不能为非继承自 MarshalByRefObject 的对象提供激活属性。The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.

- 或 --or- securityAttributes 不是 nullsecurityAttributes is not null. 未启用旧版 CAS 策略时,securityAttributes 应该为 null.When legacy CAS policy is not enabled, securityAttributes should be null.

typename 中未找到 assemblyNametypename was not found in assemblyName.

此示例为 nullThis instance is null.

注解

请参阅 assemblyName格式 AssemblyNameSee AssemblyName for the format of assemblyName.

尝试对不是当前应用程序域的目标应用程序域调用 CreateInstance 将导致成功地在目标应用程序域中加载程序集。An attempt to call CreateInstance on a target application domain that is not the current application domain will result in a successful load of the assembly in the target application domain. 由于未 MarshalByRefObjectAssembly,因此当此方法尝试将已加载程序集的 Assembly 返回到当前应用程序域时,公共语言运行时将尝试将程序集加载到当前应用程序域中,并且加载可能会失败。Since an Assembly is not MarshalByRefObject, when this method attempts to return the Assembly for the loaded assembly to the current application domain, the common language runtime will try to load the assembly into the current application domain and the load might fail. 如果两个应用程序域的路径设置不同,则加载到当前应用程序域中的程序集可能不同于首先加载的程序集。The assembly that is loaded into the current application domain might be different from the assembly that was loaded first if the path settings for the two application domains are different.

安全性

FileIOPermissionAccess
用于读取包含程序集清单的文件。for the ability to read the file containing the assembly manifest. 关联的枚举: ReadAssociated enumeration: Read

WebPermission
如果程序集不是本地的,则能够访问程序集的位置。for the ability to access the location of the assembly if the assembly is not local.

SecurityPermission
提供证据。to provide evidence. 关联的枚举:ControlEvidenceAssociated enumeration: ControlEvidence.

ReflectionPermission
用于在所有类型成员上调用操作的能力。for the ability to invoke operations on all type members. 关联的枚举: MemberAccessAssociated enumeration: MemberAccess

另请参阅

适用于