Activator.CreateComInstanceFrom 方法

定义

创建指定名称的 COM 对象的实例。Creates an instance of the COM object whose name is specified.

重载

CreateComInstanceFrom(String, String)

使用已命名的程序集文件和无参数构造函数,创建指定名称的 COM 对象的实例。Creates an instance of the COM object whose name is specified, using the named assembly file and the parameterless constructor.

CreateComInstanceFrom(String, String, Byte[], AssemblyHashAlgorithm)

使用已命名的程序集文件和无参数构造函数,创建指定名称的 COM 对象的实例。Creates an instance of the COM object whose name is specified, using the named assembly file and the parameterless constructor.

CreateComInstanceFrom(String, String)

使用已命名的程序集文件和无参数构造函数,创建指定名称的 COM 对象的实例。Creates an instance of the COM object whose name is specified, using the named assembly file and the parameterless constructor.

public:
 static System::Runtime::Remoting::ObjectHandle ^ CreateComInstanceFrom(System::String ^ assemblyName, System::String ^ typeName);
public static System.Runtime.Remoting.ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName);
static member CreateComInstanceFrom : string * string -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateComInstanceFrom (assemblyName As String, typeName As String) As ObjectHandle

参数

assemblyName
String

包含某程序集的文件的名称,将在该程序集内查找名为 typeName 的类型。The name of a file that contains an assembly where the type named typeName is sought.

typeName
String

首选类型的名称。The name of the preferred type.

返回

要访问新创建的实例则必须解包的句柄。A handle that must be unwrapped to access the newly created instance.

异常

typeNameassemblyNamenulltypeName or assemblyName is null.

无法通过 COM 创建实例。An instance cannot be created through COM.

-or- assemblyName 中未找到 typenametypename was not found in assemblyName.

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

未找到 assemblyName,或者你正在试图加载的模块未指定文件扩展名。assemblyName is not found, or the module you are trying to load does not specify a file name extension.

无法创建抽象类的实例。Cannot create an instance of an abstract class.

-or-

使用晚期绑定机制调用了此成员。This member was invoked with a late-binding mechanism.

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

assemblyName 为空字符串 ("")。assemblyName is the empty string ("").

注解

使用 ObjectHandle.Unwrap 方法将返回值解包。Use the ObjectHandle.Unwrap method to unwrap the return value.

必须显式或默认情况下将值为 true System.Runtime.InteropServices.ComVisibleAttribute 特性应用于 COM 类型,以便 CreateComInstanceFrom 方法可以创建该类型的实例;否则,会引发 TypeLoadExceptionA System.Runtime.InteropServices.ComVisibleAttribute attribute with a value of true must be applied either explicitly or by default to the COM type so the CreateComInstanceFrom method can create an instance of that type; otherwise, TypeLoadException is thrown.

有关可通过调用方法引发的其他异常的信息,请参阅 Assembly.LoadFromCreateInstance 方法的 "异常" 部分。For information about other exceptions that can be thrown by invoked methods, see the Exceptions section of the Assembly.LoadFrom and CreateInstance methods.

备注

.NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1开始,此方法可用于创建非公共类型,前提是已向调用方授予了 ReflectionPermissionFlag.RestrictedMemberAccess 标志 ReflectionPermission 并且包含非公共类型的程序集的授予集限制为调用方的授予集或reof.Starting with the .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, this method can be used to create nonpublic types if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the assembly that contains the nonpublic types is restricted to the caller's grant set or to a subset thereof. (请参阅反射的安全注意事项。)(See Security Considerations for Reflection.)

若要使用此功能,应用程序应为 .NET Framework 3.5.NET Framework 3.5 或更高版本。To use this functionality, your application should target the .NET Framework 3.5.NET Framework 3.5 or later.

安全性

ReflectionPermission
用于访问非公共类型,而不考虑其授予集。for accessing nonpublic types regardless of their grant set. 关联的枚举: MemberAccessAssociated enumeration: MemberAccess

CreateComInstanceFrom(String, String, Byte[], AssemblyHashAlgorithm)

使用已命名的程序集文件和无参数构造函数,创建指定名称的 COM 对象的实例。Creates an instance of the COM object whose name is specified, using the named assembly file and the parameterless constructor.

public:
 static System::Runtime::Remoting::ObjectHandle ^ CreateComInstanceFrom(System::String ^ assemblyName, System::String ^ typeName, cli::array <System::Byte> ^ hashValue, System::Configuration::Assemblies::AssemblyHashAlgorithm hashAlgorithm);
public static System.Runtime.Remoting.ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName, byte[] hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
static member CreateComInstanceFrom : string * string * byte[] * System.Configuration.Assemblies.AssemblyHashAlgorithm -> System.Runtime.Remoting.ObjectHandle
Public Shared Function CreateComInstanceFrom (assemblyName As String, typeName As String, hashValue As Byte(), hashAlgorithm As AssemblyHashAlgorithm) As ObjectHandle

参数

assemblyName
String

包含某程序集的文件的名称,将在该程序集内查找名为 typeName 的类型。The name of a file that contains an assembly where the type named typeName is sought.

typeName
String

首选类型的名称。The name of the preferred type.

hashValue
Byte[]

计算所得的哈希代码的值。The value of the computed hash code.

hashAlgorithm
AssemblyHashAlgorithm

用于对文件进行哈希计算并生成强名称的哈希算法。The hash algorithm used for hashing files and generating the strong name.

返回

要访问新创建的实例则必须解包的句柄。A handle that must be unwrapped to access the newly created instance.

异常

typeNameassemblyNamenulltypeName or assemblyName is null.

assemblyName 为空字符串 ("")。assemblyName is the empty string ("").

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

-or- assemblyName 超过系统定义的最大长度。assemblyName is longer than the system-defined maximum length.

未找到 assemblyName,或者你正在试图加载的模块未指定文件扩展名。assemblyName is not found, or the module you are trying to load does not specify a file name extension.

assemblyName 已找到,但无法加载。assemblyName is found but cannot be loaded.

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

指定了不以“file://”开头的基本代码,且没有所需的 WebPermissionA code base that does not start with "file://" was specified without the required WebPermission.

无法通过 COM 创建实例。An instance cannot be created through COM.

-or- assemblyName 中未找到 typenametypename was not found in assemblyName.

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

无法创建抽象类的实例。An instance of an abstract class cannot be created.

-or-

使用晚期绑定机制调用了此成员。This member was invoked with a late-binding mechanism.

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

注解

使用 ObjectHandle.Unwrap 方法将返回值解包。Use the ObjectHandle.Unwrap method to unwrap the return value.

必须显式或默认情况下将值为 true System.Runtime.InteropServices.ComVisibleAttribute 特性应用于 COM 类型,以便 CreateComInstanceFrom 方法可以创建该类型的实例;否则,会引发 TypeLoadExceptionA System.Runtime.InteropServices.ComVisibleAttribute attribute with a value of true must be applied either explicitly or by default to the COM type so the CreateComInstanceFrom method can create an instance of that type; otherwise, TypeLoadException is thrown.

有关可通过调用方法引发的其他异常的信息,请参阅 Assembly.LoadFromCreateInstance 方法的 "异常" 部分。For information about other exceptions that can be thrown by invoked methods, see the Exceptions section of the Assembly.LoadFrom and CreateInstance methods.

备注

.NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1开始,此方法可用于创建非公共类型,前提是已向调用方授予了 ReflectionPermissionFlag.RestrictedMemberAccess 标志 ReflectionPermission 并且包含非公共类型的程序集的授予集限制为调用方的授予集或reof.Starting with the .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, this method can be used to create nonpublic types if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the assembly that contains the nonpublic types is restricted to the caller's grant set or to a subset thereof. (请参阅反射的安全注意事项。)(See Security Considerations for Reflection.)

若要使用此功能,应用程序应为 .NET Framework 3.5.NET Framework 3.5 或更高版本。To use this functionality, your application should target the .NET Framework 3.5.NET Framework 3.5 or later.

安全性

ReflectionPermission
用于访问非公共类型,而不考虑其授予集。for accessing nonpublic types regardless of their grant set. 关联的枚举: MemberAccessAssociated enumeration: MemberAccess

FileIOPermission
用于读取以 "file://" 开头的 URI。for reading a URI that begins with "file://". 关联的枚举: ReadAssociated enumeration: Read

WebPermission
用于读取不以 "file://" 开头的 URI。for reading a URI that does not begin with "file://".

适用于