Activator.CreateInstance 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用最符合指定参数的构造函数创建指定类型的实例。
重载
CreateInstance(AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
注意
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:
static System::Runtime::Remoting::ObjectHandle ^ CreateInstance(AppDomain ^ domain, 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);
public static System.Runtime.Remoting.ObjectHandle CreateInstance (AppDomain domain, 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);
[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.")]
[System.Security.SecurityCritical]
public static System.Runtime.Remoting.ObjectHandle CreateInstance (AppDomain domain, 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);
static member CreateInstance : AppDomain * string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
[<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.")>]
[<System.Security.SecurityCritical>]
static member CreateInstance : AppDomain * string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstance (domain As AppDomain, assemblyName As String, typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object(), securityAttributes As Evidence) As ObjectHandle
参数
- domain
- AppDomain
在其中创建名为 typeName
的类型的域。
- assemblyName
- String
将在其中查找名为 typeName
的类型的程序集的名称。 如果 assemblyName
为 null
,则搜索正在执行的程序集。
- typeName
- String
为其创建实例的类型的完全限定名称。
- ignoreCase
- Boolean
使用 true
指定对 typeName
的搜索不区分大小写;使用 false
则指定搜索区分大小写。
- bindingAttr
- BindingFlags
影响 typeName
构造函数搜索的零个或多个位标志的组合。 如果 bindingAttr
为零,则对公共构造函数进行区分大小写的搜索。
- binder
- Binder
使用 bindingAttr
和 args
来查找和标识 typeName
构造函数的对象。 如果 binder
为 null
,则使用默认联编程序。
- args
- Object[]
与要调用的构造函数的参数的编号、顺序和类型匹配的参数数组。 如果 args
为空数组或 null
,则调用不带任何参数的构造函数(无参数构造函数)。
- culture
- CultureInfo
区域性特定的信息,这些信息控制将 args
强制转换为 typeName
构造函数所声明的正式类型。 如果 culture
为 null
,则使用当前线程的 CultureInfo。
- activationAttributes
- Object[]
包含一个或多个可以参与激活的特性的数组。 通常为包含单个 UrlAttribute 对象的数组。 UrlAttribute 指定激活远程对象所需的 URL。
- securityAttributes
- Evidence
用于做出安全策略决策和授予代码权限的信息。
返回
必须取消包装才能访问新创建的对象或 null
无 Nullable<T> 值的实例的句柄。
- 属性
例外
domain
或 typeName
为 null
。
未找到匹配的构造函数。
在 assemblyName
中未找到 typename
。
未找到 assemblyName
。
调用方没有权限调用此构造函数。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
通过反射调用的构造函数引发异常。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
activationAttributes
不是空数组,且正在创建的类型不是从 MarshalByRefObject 派生得来的。
- 或 -
最匹配 args
的构造函数具有 varargs
参数。
assemblyName
不是有效的程序集。
- 或 -
当前已加载公共语言运行时 (CLR) 2.0 或更新版本,但 assemblyName
是使用比当前加载版本更新的 CLR 版本编译的。 请注意,.NET Framework 2.0、3.0 和 3.5 都使用 CLR 2.0。
注解
当主机需要在具有受限安全权限的应用程序域中执行代码时使用 CreateInstance 。
使用 ObjectHandle.Unwrap 取消包装返回值。
适用于
CreateInstance(AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])
使用命名的程序集和最匹配所指定参数的构造函数,来创建其名称在指定的远程域中指定的类型的实例。
public:
static System::Runtime::Remoting::ObjectHandle ^ CreateInstance(AppDomain ^ domain, 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.SecurityCritical]
public static System.Runtime.Remoting.ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes);
[<System.Security.SecurityCritical>]
static member CreateInstance : AppDomain * string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstance (domain As AppDomain, assemblyName As String, typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object()) As ObjectHandle
参数
- domain
- AppDomain
在其中创建名为 typeName
的类型的域。
- assemblyName
- String
将在其中查找名为 typeName
的类型的程序集的名称。 如果 assemblyName
为 null
,则搜索正在执行的程序集。
- typeName
- String
为其创建实例的类型的完全限定名称。
- ignoreCase
- Boolean
使用 true
指定对 typeName
的搜索不区分大小写;使用 false
则指定搜索区分大小写。
- bindingAttr
- BindingFlags
影响 typeName
构造函数搜索的零个或多个位标志的组合。 如果 bindingAttr
为零,则对公共构造函数进行区分大小写的搜索。
- binder
- Binder
使用 bindingAttr
和 args
来查找和标识 typeName
构造函数的对象。 如果 binder
为 null
,则使用默认联编程序。
- args
- Object[]
与要调用的构造函数的参数的编号、顺序和类型匹配的参数数组。 如果 args
为空数组或 null
,则调用不带任何参数的构造函数(无参数构造函数)。
- culture
- CultureInfo
区域性特定的信息,这些信息控制将 args
强制转换为 typeName
构造函数所声明的正式类型。 如果 culture
为 null
,则使用当前线程的 CultureInfo。
- activationAttributes
- Object[]
包含一个或多个可以参与激活的特性的数组。 这通常为包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。
此参数与客户端激活的对象相关。 客户端激活是一项传统技术,保留用于向后兼容,但不建议用于新的开发。 应改用 Windows Communication Foundation 来开发分布式应用程序。
返回
必须取消包装才能访问新创建的对象或 null
无 Nullable<T> 值的实例的句柄。
- 属性
例外
domain
或 typeName
为 null
。
未找到匹配的构造函数。
在 assemblyName
中未找到 typename
。
未找到 assemblyName
。
调用方没有权限调用此构造函数。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
通过反射调用的构造函数引发异常。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
activationAttributes
不是空数组,且正在创建的类型不是从 MarshalByRefObject 派生得来的。
- 或 -
最匹配 args
的构造函数具有 varargs
参数。
assemblyName
不是有效的程序集。
- 或 -
当前已加载公共语言运行时 (CLR) 2.0 或更新版本,但 assemblyName
是使用比当前加载版本更新的 CLR 版本编译的。 请注意,.NET Framework 2.0、3.0 和 3.5 都使用 CLR 2.0。
注解
当主机需要在具有受限安全权限的应用程序域中执行代码时使用 CreateInstance 。
使用 ObjectHandle.Unwrap 取消包装返回值。
适用于
CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
使用指定的程序集和与指定参数匹配程度最高的构造函数来创建指定名称的类型的实例。
public:
static 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 static 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);
public static 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);
static member CreateInstance : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstance (assemblyName As String, typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object()) As ObjectHandle
参数
- assemblyName
- String
将在其中查找名为 typeName
的类型的程序集的名称。 如果 assemblyName
为 null
,则搜索正在执行的程序集。
- typeName
- String
为其创建实例的类型的完全限定名称。
- ignoreCase
- Boolean
使用 true
指定对 typeName
的搜索不区分大小写;使用 false
则指定搜索区分大小写。
- bindingAttr
- BindingFlags
影响 typeName
构造函数搜索的零个或多个位标志的组合。 如果 bindingAttr
为零,则对公共构造函数进行区分大小写的搜索。
- binder
- Binder
使用 bindingAttr
和 args
来查找和标识 typeName
构造函数的对象。 如果 binder
为 null
,则使用默认联编程序。
- args
- Object[]
与要调用的构造函数的参数的编号、顺序和类型匹配的参数数组。 如果 args
为空数组或 null
,则调用不带任何参数的构造函数(无参数构造函数)。
- culture
- CultureInfo
区域性特定的信息,这些信息控制将 args
强制转换为 typeName
构造函数所声明的正式类型。 如果 culture
为 null
,则使用当前线程的 CultureInfo。
- activationAttributes
- Object[]
包含一个或多个可以参与激活的特性的数组。 这通常为包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。
此参数与客户端激活的对象相关。 客户端激活是一项传统技术,保留用于向后兼容,但不建议用于新的开发。 应改用 Windows Communication Foundation 来开发分布式应用程序。
返回
一个必须取消包装才能访问新创建的实例的句柄,或者 null
对于 Nullable<T> 没有值的实例。
例外
typeName
为 null
。
未找到匹配的构造函数。
在 assemblyName
中未找到 typename
。
未找到 assemblyName
。
调用方没有权限调用此构造函数。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
通过反射调用的构造函数引发异常。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
activationAttributes
不是空数组,且正在创建的类型不是从 MarshalByRefObject 派生得来的。
- 或 -
最匹配 args
的构造函数具有 varargs
参数。
assemblyName
不是有效的程序集。
- 或 -
当前已加载公共语言运行时 (CLR) 2.0 或更新版本,但 assemblyName
是使用比当前加载版本更新的 CLR 版本编译的。 请注意,.NET Framework 2.0、3.0 和 3.5 都使用 CLR 2.0。
注解
使用 ObjectHandle.Unwrap 取消包装返回值。
注意
从 .NET Framework 2.0 开始,如果调用方已使用 标志授予调用ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess方,并且包含非公共类型和成员的程序集的授予集限制为调用方授权集或其子集,则此方法可用于创建非公共类型和成员。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo, Object[])
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
使用最符合指定参数的构造函数创建指定类型的实例。
public:
static System::Object ^ CreateInstance(Type ^ type, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ args, System::Globalization::CultureInfo ^ culture, cli::array <System::Object ^> ^ activationAttributes);
public static object? CreateInstance (Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes);
public static object CreateInstance (Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes);
static member CreateInstance : Type * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> obj
Public Shared Function CreateInstance (type As Type, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object()) As Object
参数
- type
- Type
要创建的对象的类型。
- bindingAttr
- BindingFlags
影响 type
构造函数搜索的零个或多个位标志的组合。 如果 bindingAttr
为零,则对公共构造函数进行区分大小写的搜索。
- binder
- Binder
使用 bindingAttr
和 args
来查找和标识 type
构造函数的对象。 如果 binder
为 null
,则使用默认联编程序。
- args
- Object[]
与要调用的构造函数的参数的编号、顺序和类型匹配的参数数组。 如果 args
为空数组或 null
,则调用不带任何参数的构造函数(无参数构造函数)。
- culture
- CultureInfo
区域性特定的信息,这些信息控制将 args
强制转换为 type
构造函数所声明的正式类型。 如果 culture
为 null
,则使用当前线程的 CultureInfo。
- activationAttributes
- Object[]
包含一个或多个可以参与激活的特性的数组。 这通常为包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。
此参数与客户端激活的对象相关。 客户端激活是一项传统技术,保留用于向后兼容,但不建议用于新的开发。 应改用 Windows Communication Foundation 来开发分布式应用程序。
返回
对新创建的 对象的引用,或者 null
对于 Nullable<T> 没有值的实例。
例外
type
为 null
。
type
不能为 TypeBuilder。
- 或 -
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
activationAttributes
不是空数组,且正在创建的类型不是从 MarshalByRefObject 派生得来的。
- 或 -
包含 type
的程序集是一个用 Save 创建的动态程序集。
- 或 -
最匹配 args
的构造函数具有 varargs
参数。
正在被调用的构造函数引发了一个异常。
调用方没有权限调用此构造函数。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
未找到匹配的构造函数。
type
是一个 COM 对象,但用于获取类型的类标识符无效,或标识的类未注册。
type
不是有效类型。
注解
要调用的构造函数必须在指定绑定器和绑定属性的约束下提供具有指定参数列表的最具体匹配项。
注意
从 .NET Framework 2.0 开始,如果调用方已使用 标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess调用方,并且非公共类型和成员的授予集仅限于调用方授权集或其子集,则此方法可用于访问非公共类型和成员。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo)
- Source:
- Activator.cs
- Source:
- Activator.cs
- Source:
- Activator.cs
使用最符合指定参数的构造函数创建指定类型的实例。
public:
static System::Object ^ CreateInstance(Type ^ type, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ args, System::Globalization::CultureInfo ^ culture);
public static object? CreateInstance (Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture);
public static object CreateInstance (Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture);
static member CreateInstance : Type * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo -> obj
Public Shared Function CreateInstance (type As Type, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo) As Object
参数
- type
- Type
要创建的对象的类型。
- bindingAttr
- BindingFlags
影响 type
构造函数搜索的零个或多个位标志的组合。 如果 bindingAttr
为零,则对公共构造函数进行区分大小写的搜索。
- binder
- Binder
使用 bindingAttr
和 args
来查找和标识 type
构造函数的对象。 如果 binder
为 null
,则使用默认联编程序。
- args
- Object[]
与要调用的构造函数的参数的编号、顺序和类型匹配的参数数组。 如果 args
为空数组或 null
,则调用不带任何参数的构造函数(无参数构造函数)。
- culture
- CultureInfo
区域性特定的信息,这些信息控制将 args
强制转换为 type
构造函数所声明的正式类型。 如果 culture
为 null
,则使用当前线程的 CultureInfo。
返回
对新创建的 对象的引用,或者 null
对于 Nullable<T> 没有值的实例。
例外
type
为 null
。
type
不能为 TypeBuilder。
- 或 -
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
包含 type
的程序集是一个用 Save 创建的动态程序集。
- 或 -
最匹配 args
的构造函数具有 varargs
参数。
正在被调用的构造函数引发了一个异常。
调用方没有权限调用此构造函数。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
未找到匹配的构造函数。
type
是一个 COM 对象,但用于获取类型的类标识符无效,或标识的类未注册。
type
不是有效类型。
注解
要调用的构造函数必须在指定绑定器和绑定属性的约束下提供具有指定参数列表的最具体匹配项。
注意
从 .NET Framework 2.0 开始,如果已向调用方授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess标志,并且包含非公共类型和成员的程序集的授予集限制为调用方授权集或其子集,则此方法可用于访问非公共类型和成员。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
CreateInstance(Type, Object[], Object[])
- Source:
- Activator.cs
- Source:
- Activator.cs
- Source:
- Activator.cs
使用最符合指定参数的构造函数创建指定类型的实例。
public:
static System::Object ^ CreateInstance(Type ^ type, cli::array <System::Object ^> ^ args, cli::array <System::Object ^> ^ activationAttributes);
public static object? CreateInstance (Type type, object?[]? args, object?[]? activationAttributes);
public static object CreateInstance (Type type, object[] args, object[] activationAttributes);
static member CreateInstance : Type * obj[] * obj[] -> obj
Public Shared Function CreateInstance (type As Type, args As Object(), activationAttributes As Object()) As Object
参数
- type
- Type
要创建的对象的类型。
- args
- Object[]
与要调用的构造函数的参数的编号、顺序和类型匹配的参数数组。 如果 args
为空数组或 null
,则调用不带任何参数的构造函数(无参数构造函数)。
- activationAttributes
- Object[]
包含一个或多个可以参与激活的特性的数组。 这通常为包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。
此参数与客户端激活的对象相关。 客户端激活是一项传统技术,保留用于向后兼容,但不建议用于新的开发。 应改用 Windows Communication Foundation 来开发分布式应用程序。
返回
对新创建的 对象的引用,或者 null
对于 Nullable<T> 没有值的实例。
例外
type
为 null
。
type
不能为 TypeBuilder。
- 或 -
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
activationAttributes
不是空数组,且正在创建的类型不是从 MarshalByRefObject 派生得来的。
- 或 -
包含 type
的程序集是一个用 Save 创建的动态程序集。
- 或 -
最匹配 args
的构造函数具有 varargs
参数。
正在被调用的构造函数引发了一个异常。
调用方没有权限调用此构造函数。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
未找到匹配的公共构造函数。
type
是一个 COM 对象,但用于获取类型的类标识符无效,或标识的类未注册。
type
不是有效类型。
注解
要调用的构造函数必须可访问,并且必须使用指定的参数列表提供最具体的匹配项。
注意
从 .NET Framework 2.0 开始,如果已向调用方授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess标志,并且包含非公共类型的程序集的授予集仅限于调用方授权集或其子集,则此方法可用于访问非公共类型。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
CreateInstance(String, String, Object[])
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
使用已命名的程序集和无参数构造函数,创建名称已指定的类型的实例。
public:
static System::Runtime::Remoting::ObjectHandle ^ CreateInstance(System::String ^ assemblyName, System::String ^ typeName, cli::array <System::Object ^> ^ activationAttributes);
public static System.Runtime.Remoting.ObjectHandle? CreateInstance (string assemblyName, string typeName, object?[]? activationAttributes);
public static System.Runtime.Remoting.ObjectHandle CreateInstance (string assemblyName, string typeName, object[] activationAttributes);
static member CreateInstance : string * string * obj[] -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstance (assemblyName As String, typeName As String, activationAttributes As Object()) As ObjectHandle
参数
- assemblyName
- String
将在其中查找名为 typeName
的类型的程序集的名称。 如果 assemblyName
为 null
,则搜索正在执行的程序集。
- typeName
- String
为其创建实例的类型的完全限定名称。
- activationAttributes
- Object[]
包含一个或多个可以参与激活的特性的数组。 这通常为包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。
此参数与客户端激活的对象相关。 客户端激活是一项传统技术,保留用于向后兼容,但不建议用于新的开发。 应改用 Windows Communication Foundation 来开发分布式应用程序。
返回
必须解包才能访问新创建的对象或 null
实例 Nullable<T> 的句柄。
例外
typeName
为 null
。
未找到匹配的公共构造函数。
在 assemblyName
中未找到 typename
。
未找到 assemblyName
。
调用方没有权限调用此构造函数。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
activationAttributes
不是空数组,且正在创建的类型不是从 MarshalByRefObject 派生得来的。
- 或 -
activationAttributes
不是 UrlAttribute。
数组。
assemblyName
不是有效的程序集。
- 或 -
当前已加载公共语言运行时 (CLR) 2.0 或更新版本,但 assemblyName
是使用比当前加载版本更新的 CLR 版本编译的。 请注意,.NET Framework 2.0、3.0 和 3.5 都使用 CLR 2.0。
注解
使用 ObjectHandle.Unwrap 解包返回值。
注意
从 .NET Framework 2.0 开始,如果调用方已使用 标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess调用方,并且非公共类型的授予集仅限于调用者的授权集或其子集,则此方法可用于创建非公共类型。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
注意
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:
static 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 ^ securityInfo);
public static 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 securityInfo);
[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 static 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 securityInfo);
static member CreateInstance : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
[<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.")>]
static member CreateInstance : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstance (assemblyName As String, typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object(), securityInfo As Evidence) As ObjectHandle
参数
- assemblyName
- String
将在其中查找名为 typeName
的类型的程序集的名称。 如果 assemblyName
为 null
,则搜索正在执行的程序集。
- typeName
- String
为其创建实例的类型的完全限定名称。
- ignoreCase
- Boolean
使用 true
指定对 typeName
的搜索不区分大小写;使用 false
则指定搜索区分大小写。
- bindingAttr
- BindingFlags
影响 typeName
构造函数搜索的零个或多个位标志的组合。 如果 bindingAttr
为零,则对公共构造函数进行区分大小写的搜索。
- binder
- Binder
使用 bindingAttr
和 args
来查找和标识 typeName
构造函数的对象。 如果 binder
为 null
,则使用默认联编程序。
- args
- Object[]
与要调用的构造函数的参数的编号、顺序和类型匹配的参数数组。 如果 args
为空数组或 null
,则调用不带任何参数的构造函数(无参数构造函数)。
- culture
- CultureInfo
区域性特定的信息,这些信息控制将 args
强制转换为 typeName
构造函数所声明的正式类型。 如果 culture
为 null
,则使用当前线程的 CultureInfo。
- activationAttributes
- Object[]
包含一个或多个可以参与激活的特性的数组。 这通常为包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。
此参数与客户端激活的对象相关。 客户端激活是一项传统技术,保留用于向后兼容,但不建议用于新的开发。 应改用 Windows Communication Foundation 来开发分布式应用程序。
- securityInfo
- Evidence
用于做出安全策略决策和授予代码权限的信息。
返回
一个必须解包才能访问新创建的 对象的句柄,或者 null
对于 Nullable<T> 没有值的实例。
- 属性
例外
typeName
为 null
。
未找到匹配的构造函数。
在 assemblyName
中未找到 typename
。
未找到 assemblyName
。
调用方没有权限调用此构造函数。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
通过反射调用的构造函数引发异常。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
activationAttributes
不是空数组,且正在创建的类型不是从 MarshalByRefObject 派生得来的。
- 或 -
最匹配 args
的构造函数具有 varargs
参数。
assemblyName
不是有效的程序集。
- 或 -
当前已加载公共语言运行时 (CLR) 2.0 或更新版本,但 assemblyName
是使用比当前加载版本更新的 CLR 版本编译的。 请注意,.NET Framework 2.0、3.0 和 3.5 都使用 CLR 2.0。
注解
使用 ObjectHandle.Unwrap 解包返回值。
注意
从 .NET Framework 2.0 开始,如果调用方已使用 标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess调用方,并且包含非公共类型和成员的程序集的授予集限制为调用方授权集或其子集,则此方法可用于创建非公共类型和成员。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
CreateInstance(Type, Object[])
- Source:
- Activator.cs
- Source:
- Activator.cs
- Source:
- Activator.cs
使用最符合指定参数的构造函数创建指定类型的实例。
public:
static System::Object ^ CreateInstance(Type ^ type, ... cli::array <System::Object ^> ^ args);
public:
static System::Object ^ CreateInstance(Type ^ type, cli::array <System::Object ^> ^ args);
public static object CreateInstance (Type type, params object[] args);
public static object? CreateInstance (Type type, params object?[]? args);
public static object CreateInstance (Type type, object[] args);
static member CreateInstance : Type * obj[] -> obj
Public Shared Function CreateInstance (type As Type, ParamArray args As Object()) As Object
Public Shared Function CreateInstance (type As Type, args As Object()) As Object
参数
- type
- Type
要创建的对象的类型。
- args
- Object[]
与要调用的构造函数的参数的编号、顺序和类型匹配的参数数组。 如果 args
为空数组或 null
,则调用不带任何参数的构造函数(无参数构造函数)。
返回
对新创建的 对象的引用,或 null
对 Nullable<T> 没有值的实例的引用。
例外
type
为 null
。
type
不能为 TypeBuilder。
- 或 -
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
包含 type
的程序集是一个用 Save 创建的动态程序集。
- 或 -
最匹配 args
的构造函数具有 varargs
参数。
正在被调用的构造函数引发了一个异常。
调用方没有权限调用此构造函数。
注意:在 适用于 Windows 应用商店应用的 .NET 或 可移植类库中,改为捕获基类异常 MemberAccessException。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
未找到匹配的公共构造函数。
注意:在 适用于 Windows 应用商店应用的 .NET 或 可移植类库中,改为捕获基类异常 MissingMemberException。
type
是一个 COM 对象,但用于获取类型的类标识符无效,或标识的类未注册。
type
不是有效类型。
示例
以下示例调用 CreateInstance(Type, Object[]) 方法来创建 String 对象。 它调用 String.String(Char[], Int32, Int32) 构造函数来实例化一个字符串,该字符串包含从第 14 个位置开始的字符数组中的 10 个元素。
using System;
public class Example
{
public static void Main()
{
// Initialize array of characters from a to z.
char[] chars = new char[26];
for (int ctr = 0; ctr < 26; ctr++)
chars[ctr] = (char) (ctr + 0x0061);
object obj = Activator.CreateInstance(typeof(string),
new object[] { chars, 13, 10 } );
Console.WriteLine(obj);
}
}
// The example displays the following output:
// nopqrstuvw
open System
// Initialize array of characters from a to z.
let chars = [| 'a' .. 'z' |]
let obj = Activator.CreateInstance(typeof<string>, chars[13..22])
printfn $"{obj}"
// The example displays the following output:
// nopqrstuvw
Module Example
Public Sub Main()
' Initialize array of characters from a to z.
Dim chars(25) As Char
For ctr As Short = 0 To 25
chars(ctr) = ChrW(ctr + &h0061)
Next
Dim obj As Object = Activator.CreateInstance(GetType(String),
{ chars, 13, 10 })
Console.WriteLine(obj)
End Sub
End Module
' The example displays the following output:
' nopqrstuvw
以下示例创建一个交错数组,其元素是要传递给 String 构造函数的参数。 然后,该示例将每个数组传递给 CreateInstance(Type, Object[]) 方法,以调用相应的字符串构造函数。
using System;
public class Example
{
public static void Main()
{
char[] characters = { 'a', 'b', 'c', 'd', 'e', 'f' };
object[][] arguments = new object[3][] { new object[] { characters },
new object[] { characters, 1, 4 },
new object[] { characters[1], 20 } };
for (int ctr = 0; ctr <= arguments.GetUpperBound(0); ctr++) {
object[] args = arguments[ctr];
object result = Activator.CreateInstance(typeof(string), args);
Console.WriteLine("{0}: {1}", result.GetType().Name, result);
}
}
}
// The example displays the following output:
// String: abcdef
// String: bcde
// String: bbbbbbbbbbbbbbbbbbbb
open System
let chars = [| 'a' .. 'f' |]
let arguments =
[| chars
chars[1..4]
Array.create 20 chars[1] |]
for args in arguments do
let result =
Activator.CreateInstance(typeof<string>, args)
printfn $"{result.GetType().Name}: {result}"
// The example displays the following output:
// String: abcdef
// String: bcde
// String: bbbbbbbbbbbbbbbbbbbb
Module Example
Public Sub Main()
Dim characters() As Char = { "a"c, "b"c, "c"c, "d"c, "e"c, "f"c }
Dim arguments()() As Object = new Object(2)() { New Object() { characters },
New Object() { characters, 1, 4 },
New Object() { characters(1), 20 } }
For ctr As Integer = 0 To arguments.GetUpperBound(0)
Dim args() As Object = arguments(ctr)
Dim result As Object = Activator.CreateInstance(GetType(String), args)
Console.WriteLine("{0}: {1}", result.GetType().Name, result)
Next
End Sub
End Module
' The example displays the following output:
' String: abcdef
' String: bcde
' String: bbbbbbbbbbbbbbbbbbbb
注解
要调用的构造函数必须可访问,并且必须提供与指定参数列表最具体的匹配项。
注意
从 .NET Framework 2.0 开始,如果调用方已使用 标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess,并且包含非公共类型的程序集的授权集仅限于调用方的授权集或其子集,则此方法可用于访问非公共类型。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
CreateInstance(Type, Boolean)
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
使用类型的无参数构造函数创建指定类型的实例。
public:
static System::Object ^ CreateInstance(Type ^ type, bool nonPublic);
public static object? CreateInstance (Type type, bool nonPublic);
public static object CreateInstance (Type type, bool nonPublic);
static member CreateInstance : Type * bool -> obj
Public Shared Function CreateInstance (type As Type, nonPublic As Boolean) As Object
参数
- type
- Type
要创建的对象的类型。
- nonPublic
- Boolean
如果公共或非公共无参数构造函数可以匹配,则为 true
;如果只有公共无参数构造函数可以匹配,则为 false
。
返回
对新创建的 对象的引用,或 null
实例 Nullable<T> 的引用。
例外
type
为 null
。
type
不能为 TypeBuilder。
- 或 -
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
包含 type
的程序集是一个用 Save 创建的动态程序集。
正在被调用的构造函数引发了一个异常。
调用方没有权限调用此构造函数。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
未找到匹配的公共构造函数。
type
是一个 COM 对象,但用于获取类型的类标识符无效,或标识的类未注册。
type
不是有效类型。
注解
注意
从 .NET Framework 2.0 开始,如果已向调用方授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess标志,并且包含非公共类型和成员的程序集的授予集限制为调用方授权集或其子集,则此方法可用于访问非公共类型和成员。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
CreateInstance(String, String)
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
使用已命名的程序集和无参数构造函数,创建名称已指定的类型的实例。
public:
static System::Runtime::Remoting::ObjectHandle ^ CreateInstance(System::String ^ assemblyName, System::String ^ typeName);
public static System.Runtime.Remoting.ObjectHandle? CreateInstance (string assemblyName, string typeName);
public static System.Runtime.Remoting.ObjectHandle CreateInstance (string assemblyName, string typeName);
static member CreateInstance : string * string -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstance (assemblyName As String, typeName As String) As ObjectHandle
参数
- assemblyName
- String
将在其中查找名为 typeName
的类型的程序集的名称。 如果 assemblyName
为 null
,则搜索正在执行的程序集。
- typeName
- String
为其创建实例的类型的完全限定名称。
返回
必须取消包装才能访问新创建的对象或 null
实例 Nullable<T> 的句柄。
例外
typeName
为 null
。
未找到匹配的公共构造函数。
在 assemblyName
中未找到 typename
。
未找到 assemblyName
。
调用方没有权限调用此构造函数。
你无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
通过反射调用的构造函数引发异常。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
assemblyName
不是有效的程序集。
- 或 -
当前已加载公共语言运行时 (CLR) 2.0 或更新版本,但 assemblyName
是使用比当前加载版本更新的 CLR 版本编译的。 请注意,.NET Framework 2.0、3.0 和 3.5 都使用 CLR 2.0。
示例
以下示例在名为 的 Person
程序集中定义名为 的 PersonInfo
类。 请注意, Person
类有两个构造函数,其中一个是无参数的。
using System;
public class Person
{
private string _name;
public Person()
{ }
public Person(string name)
{
this._name = name;
}
public string Name
{ get { return this._name; }
set { this._name = value; } }
public override string ToString()
{
return this._name;
}
}
type Person(name) =
member val Name = name with get, set
override this.ToString() = this.Name
new () = Person Unchecked.defaultof<string>
Public Class Person
Private _name As String
Public Sub New()
End Sub
Public Sub New(name As String)
Me._name = name
End Sub
Public Property Name As String
Get
Return Me._name
End Get
Set
Me._name = value
End Set
End Property
Public Overrides Function ToString() As String
Return Me._name
End Function
End Class
以下示例调用 CreateInstance(String, String) 方法来实例化 Person
类。 它需要对 PersonInfo.dll 的引用才能添加到项目中。 CreateInstance(String, String)由于 方法调用Person
类无参数构造函数,因此该示例为其Name
属性赋值。
using System;
using System.Runtime.Remoting;
public class Example
{
public static void Main()
{
ObjectHandle handle = Activator.CreateInstance("PersonInfo", "Person");
Person p = (Person) handle.Unwrap();
p.Name = "Samuel";
Console.WriteLine(p);
}
}
// The example displays the following output:
// Samuel
open System
let handle = Activator.CreateInstance("PersonInfo", "Person")
let p = handle.Unwrap() :?> Person
p.Name <- "Samuel"
printfn $"{p}"
// The example displays the following output:
// Samuel
Imports System.Runtime.Remoting
Module Example
Public Sub Main()
Dim handle As ObjectHandle = Activator.CreateInstance("PersonInfo", "Person")
Dim p As Person = CType(handle.Unwrap(), Person)
p.Name = "Samuel"
Console.WriteLine(p)
End Sub
End Module
' The example displays the following output:
' Samuel
但是, CreateInstance 经常调用 来实例化跨越计算机边界或在设计时未知的类型。 在这种情况下,不能在项目中包括对程序集的引用,也不能对类型的成员进行早期绑定调用。 为了解决此限制,以下示例使用 CreateInstance 方法以及反射为 Person
对象的 Name
属性赋值并显示其值。
using System;
using System.Reflection;
using System.Runtime.Remoting;
public class Example
{
public static void Main()
{
ObjectHandle handle = Activator.CreateInstance("PersonInfo", "Person");
object p = handle.Unwrap();
Type t = p.GetType();
PropertyInfo prop = t.GetProperty("Name");
if (prop != null)
prop.SetValue(p, "Samuel");
MethodInfo method = t.GetMethod("ToString");
object retVal = method.Invoke(p, null);
if (retVal != null)
Console.WriteLine(retVal);
}
}
// The example displays the following output:
// Samuel
open System
let handle =
Activator.CreateInstance("PersonInfo", "Person")
let p = handle.Unwrap()
let t = p.GetType()
let prop = t.GetProperty "Name"
if not (isNull prop) then
prop.SetValue(p, "Samuel")
let method = t.GetMethod "ToString"
let retVal = method.Invoke(p, null)
if not (isNull retVal) then
printfn $"{retVal}"
// The example displays the following output:
// Samuel
Imports System.Reflection
Imports System.Runtime.Remoting
Module Example
Public Sub Main()
Dim handle As ObjectHandle = Activator.CreateInstance("PersonInfo", "Person")
Dim p As Object = handle.Unwrap()
Dim t As Type = p.GetType()
Dim prop As PropertyInfo = t.GetProperty("Name")
if Not prop Is Nothing Then
prop.SetValue(p, "Samuel")
End If
Dim method As MethodInfo = t.GetMethod("ToString")
Dim retVal As Object = method.Invoke(p, Nothing)
If Not retVal Is Nothing Then
Console.WriteLine(retVal)
End If
End Sub
End Module
' The example displays the following output:
' Samuel
注解
使用 ObjectHandle.Unwrap 取消包装返回值。
assemblyName
可以是以下任一项:
程序集的简单名称,不带其路径或文件扩展名。 例如,应为路径和名称为 .\bin\TypeExtensions.dll 的程序集指定
TypeExtensions
。已签名程序集的全名,由其简单名称、版本、区域性和公钥令牌组成;例如,“TypeExtensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=181869f2f7435b51”。
有关公共语言运行时如何标识和加载程序集的详细信息,请参阅 运行时如何查找程序集。 有关使用应用程序配置文件定义程序集位置的信息,请参阅 指定程序集的位置。 如果 assemblyName
找到 ,则会在默认上下文中加载它。
注意
从 .NET Framework 2.0 开始,如果调用方已使用 标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess调用方,并且包含非公共类型的程序集的授予集限制为调用方授权集或其子集,则此方法可用于创建非公共类型。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
CreateInstance(ActivationContext, String[])
创建类型的一个实例,该类型由指定的 ActivationContext 对象指定,并由指定的自定义激活数据激活。
public:
static System::Runtime::Remoting::ObjectHandle ^ CreateInstance(ActivationContext ^ activationContext, cli::array <System::String ^> ^ activationCustomData);
public static System.Runtime.Remoting.ObjectHandle CreateInstance (ActivationContext activationContext, string[] activationCustomData);
static member CreateInstance : ActivationContext * string[] -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstance (activationContext As ActivationContext, activationCustomData As String()) As ObjectHandle
参数
- activationContext
- ActivationContext
一个激活上下文对象,用于指定要创建的对象。
- activationCustomData
- String[]
一个包含自定义激活数据的 Unicode 字符串数组。
返回
必须取消包装才能访问新创建的对象或 null
实例 Nullable<T> 的句柄。
注解
ObjectHandle.Unwrap使用 方法解包返回值。
激活上下文在基于清单的激活期间用于设置域策略并提供基于应用程序的安全模型。 类 ActivationContext 包含一个 ApplicationIdentity 对象,该对象提供对应用程序清单的访问权限。 有关更多信息,请参见 ApplicationSecurityManager 类。
另请参阅
适用于
CreateInstance(Type)
- Source:
- Activator.cs
- Source:
- Activator.cs
- Source:
- Activator.cs
使用类型的无参数构造函数创建指定类型的实例。
public:
static System::Object ^ CreateInstance(Type ^ type);
public static object CreateInstance (Type type);
public static object? CreateInstance (Type type);
static member CreateInstance : Type -> obj
Public Shared Function CreateInstance (type As Type) As Object
参数
- type
- Type
要创建的对象的类型。
返回
对新创建的 对象的引用,或 null
实例 Nullable<T> 的引用。
例外
type
为 null
。
type
不能为 TypeBuilder。
- 或 -
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
- 或 -
包含 type
的程序集是一个用 Save 创建的动态程序集。
正在被调用的构造函数引发了一个异常。
调用方没有权限调用此构造函数。
注意:在 .NET for Windows 应用商店应用 或 可移植类库中,请改为捕获基类异常 MemberAccessException。
无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
未找到匹配的公共构造函数。
注意:在 .NET for Windows 应用商店应用 或 可移植类库中,请改为捕获基类异常 MissingMemberException。
type
是一个 COM 对象,但用于获取类型的类标识符无效,或标识的类未注册。
type
不是有效类型。
示例
下面的代码示例演示如何调用 CreateInstance(Type) 方法。 创建多个不同类型的实例并显示其默认值。
using namespace System;
ref class DynamicInstanceList
{
private:
static String^ instanceSpec = "System.EventArgs;System.Random;" +
"System.Exception;System.Object;System.Version";
public:
static void Main()
{
array<String^>^ instances = instanceSpec->Split(';');
Array^ instlist = Array::CreateInstance(Object::typeid, instances->Length);
Object^ item;
for (int i = 0; i < instances->Length; i++)
{
// create the object from the specification string
Console::WriteLine("Creating instance of: {0}", instances[i]);
item = Activator::CreateInstance(Type::GetType(instances[i]));
instlist->SetValue(item, i);
}
Console::WriteLine("\nObjects and their default values:\n");
for each (Object^ o in instlist)
{
Console::WriteLine("Type: {0}\nValue: {1}\nHashCode: {2}\n",
o->GetType()->FullName, o->ToString(), o->GetHashCode());
}
}
};
int main()
{
DynamicInstanceList::Main();
}
// This program will display output similar to the following:
//
// Creating instance of: System.EventArgs
// Creating instance of: System.Random
// Creating instance of: System.Exception
// Creating instance of: System.Object
// Creating instance of: System.Version
//
// Objects and their default values:
//
// Type: System.EventArgs
// Value: System.EventArgs
// HashCode: 46104728
//
// Type: System.Random
// Value: System.Random
// HashCode: 12289376
//
// Type: System.Exception
// Value: System.Exception: Exception of type 'System.Exception' was thrown.
// HashCode: 55530882
//
// Type: System.Object
// Value: System.Object
// HashCode: 30015890
//
// Type: System.Version
// Value: 0.0
// HashCode: 1048575
using System;
class DynamicInstanceList
{
private static string instanceSpec = "System.EventArgs;System.Random;" +
"System.Exception;System.Object;System.Version";
public static void Main()
{
string[] instances = instanceSpec.Split(';');
Array instlist = Array.CreateInstance(typeof(object), instances.Length);
object item;
for (int i = 0; i < instances.Length; i++)
{
// create the object from the specification string
Console.WriteLine("Creating instance of: {0}", instances[i]);
item = Activator.CreateInstance(Type.GetType(instances[i]));
instlist.SetValue(item, i);
}
Console.WriteLine("\nObjects and their default values:\n");
foreach (object o in instlist)
{
Console.WriteLine("Type: {0}\nValue: {1}\nHashCode: {2}\n",
o.GetType().FullName, o.ToString(), o.GetHashCode());
}
}
}
// This program will display output similar to the following:
//
// Creating instance of: System.EventArgs
// Creating instance of: System.Random
// Creating instance of: System.Exception
// Creating instance of: System.Object
// Creating instance of: System.Version
//
// Objects and their default values:
//
// Type: System.EventArgs
// Value: System.EventArgs
// HashCode: 46104728
//
// Type: System.Random
// Value: System.Random
// HashCode: 12289376
//
// Type: System.Exception
// Value: System.Exception: Exception of type 'System.Exception' was thrown.
// HashCode: 55530882
//
// Type: System.Object
// Value: System.Object
// HashCode: 30015890
//
// Type: System.Version
// Value: 0.0
// HashCode: 1048575
open System
let instanceSpec =
"System.EventArgs;System.Random;System.Exception;System.Object;System.Version"
let instances = instanceSpec.Split ';'
let instlist = Array.zeroCreate instances.Length
let mutable item = obj ()
for i = 0 to instances.Length - 1 do
// create the object from the specification string
printfn $"Creating instance of: {instances.[i]}"
item <- Activator.CreateInstance(Type.GetType instances.[i])
instlist.[i] <- item
printfn "\nObjects and their default values:\n"
for o in instlist do
printfn $"Type: {o.GetType().FullName}\nValue: {o}\nHashCode: {o.GetHashCode()}\n"
// This program will display output similar to the following:
//
// Creating instance of: System.EventArgs
// Creating instance of: System.Random
// Creating instance of: System.Exception
// Creating instance of: System.Object
// Creating instance of: System.Version
//
// Objects and their default values:
//
// Type: System.EventArgs
// Value: System.EventArgs
// HashCode: 46104728
//
// Type: System.Random
// Value: System.Random
// HashCode: 12289376
//
// Type: System.Exception
// Value: System.Exception: Exception of type 'System.Exception' was thrown.
// HashCode: 55530882
//
// Type: System.Object
// Value: System.Object
// HashCode: 30015890
//
// Type: System.Version
// Value: 0.0
// HashCode: 1048575
Class DynamicInstanceList
Private Shared instanceSpec As String = "System.EventArgs;System.Random;" + _
"System.Exception;System.Object;System.Version"
Public Shared Sub Main()
Dim instances() As String = instanceSpec.Split(";")
Dim instlist As Array = Array.CreateInstance(GetType(Object), instances.Length)
Dim item As Object
For i As Integer = 0 To instances.Length -1
' create the object from the specification string
Console.WriteLine("Creating instance of: {0}", instances(i))
item = Activator.CreateInstance(Type.GetType(instances(i)))
instlist.SetValue(item, i)
Next i
Console.WriteLine(Environment.NewLine + "Objects and their default values:" + Environment.NewLine)
For Each o As Object In instlist
Console.WriteLine("Type: {0}" + Environment.NewLine + "Value: {1}" + _
Environment.NewLine + "HashCode: {2}" + Environment.NewLine, _
o.GetType().FullName, o.ToString(), o.GetHashCode())
Next o
End Sub
End Class
' This program will display output similar to the following:
'
' Creating instance of: System.EventArgs
' Creating instance of: System.Random
' Creating instance of: System.Exception
' Creating instance of: System.Object
' Creating instance of: System.Version
'
' Objects and their default values:
'
' Type: System.EventArgs
' Value: System.EventArgs
' HashCode: 46104728
'
' Type: System.Random
' Value: System.Random
' HashCode: 12289376
'
' Type: System.Exception
' Value: System.Exception: Exception of type 'System.Exception' was thrown.
' HashCode: 55530882
'
' Type: System.Object
' Value: System.Object
' HashCode: 30015890
'
' Type: System.Version
' Value: 0.0
' HashCode: 1048575
注解
要调用的构造函数必须可访问。
注意
从 .NET Framework 2.0 开始,如果已向调用方授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess标志,并且包含非公共类型的程序集的授予集仅限于调用方授权集或其子集,则此方法可用于访问非公共类型。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
CreateInstance(ActivationContext)
创建类型的一个实例,该类型由指定的 ActivationContext 对象指定。
public:
static System::Runtime::Remoting::ObjectHandle ^ CreateInstance(ActivationContext ^ activationContext);
public static System.Runtime.Remoting.ObjectHandle CreateInstance (ActivationContext activationContext);
static member CreateInstance : ActivationContext -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstance (activationContext As ActivationContext) As ObjectHandle
参数
- activationContext
- ActivationContext
一个激活上下文对象,用于指定要创建的对象。
返回
必须取消包装才能访问新创建的对象或 null
实例 Nullable<T> 的句柄。
注解
ObjectHandle.Unwrap使用 方法解包返回值。
激活上下文在基于清单的激活期间用于设置域策略并提供基于应用程序的安全模型。 类 ActivationContext 包含一个 ApplicationIdentity 对象,该对象提供对应用程序清单的访问权限。 有关更多信息,请参见 ApplicationSecurityManager 类。
另请参阅
适用于
CreateInstance(AppDomain, String, String)
使用命名的程序集和无参数构造函数,来创建其名称在指定的远程域中指定的类型的实例。
public:
static System::Runtime::Remoting::ObjectHandle ^ CreateInstance(AppDomain ^ domain, System::String ^ assemblyName, System::String ^ typeName);
public static System.Runtime.Remoting.ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName);
[System.Security.SecurityCritical]
public static System.Runtime.Remoting.ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName);
static member CreateInstance : AppDomain * string * string -> System.Runtime.Remoting.ObjectHandle
[<System.Security.SecurityCritical>]
static member CreateInstance : AppDomain * string * string -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateInstance (domain As AppDomain, assemblyName As String, typeName As String) As ObjectHandle
参数
- domain
- AppDomain
在其中创建名为 typeName
的类型的远程域。
- assemblyName
- String
将在其中查找名为 typeName
的类型的程序集的名称。 如果 assemblyName
为 null
,则搜索正在执行的程序集。
- typeName
- String
为其创建实例的类型的完全限定名称。
返回
必须解包才能访问新创建的对象或 null
实例 Nullable<T> 的句柄。
- 属性
例外
typeName
或 domain
为 null
。
未找到匹配的公共构造函数。
在 assemblyName
中未找到 typename
。
未找到 assemblyName
。
调用方没有权限调用此构造函数。
通过反射调用的构造函数引发异常。
未通过 GetTypeFromProgID 或 GetTypeFromCLSID 获取 COM 类型。
不支持创建 TypedReference、ArgIterator、Void 和 RuntimeArgumentHandle 类型,或者这些类型的数组。
assemblyName
不是有效的程序集。
- 或 -
当前已加载公共语言运行时 (CLR) 2.0 或更新版本,但 assemblyName
是使用比当前加载版本更新的 CLR 版本编译的。 请注意,.NET Framework 2.0、3.0 和 3.5 都使用 CLR 2.0。
注解
当主机需要在具有受限安全权限的应用程序域中执行代码时使用 CreateInstance 。
使用 ObjectHandle.Unwrap 解包返回值。
适用于
CreateInstance<T>()
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
- Source:
- Activator.RuntimeType.cs
使用无参数构造函数,创建指定泛型类型参数所指定类型的实例。
public:
generic <typename T>
static T CreateInstance();
public static T CreateInstance<T> ();
static member CreateInstance : unit -> 'T
Public Shared Function CreateInstance(Of T) () As T
类型参数
- T
要创建的类型。
返回
对新创建的 对象的引用,或 null
实例 Nullable<T> 的引用。
例外
无法创建抽象类的实例;或者,为 T
指定的类型没有无参数的构造函数。
注意:在 适用于 Windows 应用商店应用的 .NET 或 可移植类库中,改为捕获基类异常 MissingMemberException。
注解
CreateInstance<T>()编译器使用泛型方法实现类型参数指定的类型的实例化。 例如,在以下泛型方法中,C++) 中 (gcnew T()
的实现new T()
使用 CreateInstance<T>() 泛型方法。
public:
generic <typename T> where T:gcnew()
static T Bar()
{
return gcnew T();
}
public static T Factory<T>() where T : new()
{
return new T();
}
let factory<'T when 'T : (new: unit -> 'T)> =
new 'T()
Public Shared Function Factory(Of T As New)() As T
Return New T()
End Function
通常,应用程序代码中的泛型方法没有用 CreateInstance<T>() 处,因为类型必须在编译时已知。 如果类型在编译时已知,则可以在 C# New
中的 (new
运算符、Visual Basic gcnew
中的 C++) 中使用常规实例化语法。 如果类型在编译时未知,则可以调用 的非泛型重载 CreateInstance。
没有采用参数列表的 CreateInstance<T>() 泛型方法重载,因为 的非泛型重载 CreateInstance 已提供后期绑定构造函数解析。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈