AppDomain::CreateComInstanceFrom Method (String^, String^)
Creates a new instance of a specified COM type. Parameters specify the name of a file that contains an assembly containing the type and the name of the type.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- assemblyName
-
Type:
System::String^
The name of a file containing an assembly that defines the requested type.
- typeName
-
Type:
System::String^
The name of the requested type.
Return Value
Type: System.Runtime.Remoting::ObjectHandle^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.
| Exception | Condition |
|---|---|
| ArgumentNullException | assemblyName or typeName is null. |
| TypeLoadException | The type cannot be loaded. |
| AppDomainUnloadedException | The operation is attempted on an unloaded application domain. |
| MissingMethodException | No public parameterless constructor was found. |
| FileNotFoundException | assemblyName is not found. |
| MemberAccessException | typeName is an abstract class. -or- This member was invoked with a late-binding mechanism. |
| NotSupportedException | The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject. |
| ArgumentException | assemblyName is an empty string (""). |
| BadImageFormatException | assemblyName is not a valid assembly. |
| FileLoadException | An assembly or module was loaded twice with two different evidences. |
| NullReferenceException | The COM object that is being referred to is null. |
Use this method to create objects remotely without having to load the type locally.
The return value must to be unwrapped to access the real object.
ASystem.Runtime.InteropServices::ComVisibleAttribute attribute with a value of true must be applied either explicitly or by default to the COM type for this method to create an instance of that type; otherwise, TypeLoadException is thrown.
The following sample demonstrates
using namespace System; using namespace System::Reflection; using namespace System::Runtime::InteropServices; [ComVisible(true)] public ref class MyComVisibleType { public: MyComVisibleType() { Console::WriteLine( "MyComVisibleType instantiated!" ); } }; [ComVisible(false)] public ref class MyComNonVisibleType { public: MyComNonVisibleType() { Console::WriteLine( "MyComNonVisibleType instantiated!" ); } }; void CreateComInstance( String^ typeName ) { try { AppDomain^ currentDomain = AppDomain::CurrentDomain; String^ assemblyName = currentDomain->FriendlyName; currentDomain->CreateComInstanceFrom( assemblyName, typeName ); } catch ( Exception^ e ) { Console::WriteLine( e->Message ); } } int main() { CreateComInstance( "MyComNonVisibleType" ); // Fail! CreateComInstance( "MyComVisibleType" ); // OK! }
for the ability to access the location of the assembly. Associated enumeration: FileIOPermissionAccess::PathDiscovery
for the ability to read the file containing the assembly manifest. Associated enumeration: FileIOPermissionAccess::Read
for the ability to access the location of the assembly if the assembly is not local.
Available since 1.1