AppDomain.CreateComInstanceFrom AppDomain.CreateComInstanceFrom AppDomain.CreateComInstanceFrom AppDomain.CreateComInstanceFrom Method

Definition

Creates a new instance of a specified COM type.

Overloads

CreateComInstanceFrom(String, String) CreateComInstanceFrom(String, String) CreateComInstanceFrom(String, String) CreateComInstanceFrom(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.

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

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.

CreateComInstanceFrom(String, String) CreateComInstanceFrom(String, String) CreateComInstanceFrom(String, String) CreateComInstanceFrom(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.

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

Parameters

assemblyName
String String String String

The name of a file containing an assembly that defines the requested type.

typeName
String String String String

The name of the requested type.

Returns

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.

Exceptions

typeName is an abstract class.

-or-

This member was invoked with a late-binding mechanism.

The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.

An assembly or module was loaded twice with two different evidences.

Examples

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!
}

using System;
using System.Reflection;
using System.Runtime.InteropServices;

[ComVisible(true)]
class MyComVisibleType {
   public MyComVisibleType() {
      Console.WriteLine("MyComVisibleType instantiated!");
   }
}

[ComVisible(false)]
class MyComNonVisibleType {
   public MyComNonVisibleType() {
      Console.WriteLine("MyComNonVisibleType instantiated!");
   }
}

class Test {
   public static void Main() {
      CreateComInstance("MyComNonVisibleType");   // Fail!
      CreateComInstance("MyComVisibleType");      // OK!
   }
   
   static void CreateComInstance(string typeName) {
      try {
         AppDomain currentDomain = AppDomain.CurrentDomain;
         string assemblyName = currentDomain.FriendlyName;
         currentDomain.CreateComInstanceFrom(assemblyName, typeName);
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }
}
Imports System.Reflection
Imports System.Runtime.InteropServices

<ComVisible(True)> _
Class MyComVisibleType

   Public Sub New()
      Console.WriteLine("MyComVisibleType instantiated!")
   End Sub

End Class

<ComVisible(False)> _
Class MyComNonVisibleType

   Public Sub New()
      Console.WriteLine("MyComNonVisibleType instantiated!")
   End Sub

End Class

Module Test

   Sub Main()
      CreateComInstance("MyComNonVisibleType")   ' Fail!
      CreateComInstance("MyComVisibleType")      ' OK!
   End Sub

   Sub CreateComInstance(typeName As String)
      Try
         Dim currentDomain As AppDomain = AppDomain.CurrentDomain
         Dim assemblyName As String = currentDomain.FriendlyName
         currentDomain.CreateComInstanceFrom(assemblyName, typeName)
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub

End Module 'Test

Remarks

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.

A System.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.

Security

FileIOPermissionAccess
for the ability to read the file containing the assembly manifest. Associated enumeration: Read

WebPermission
for the ability to access the location of the assembly if the assembly is not local.

See also

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

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.

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

Parameters

assemblyFile
String String String String

The name of a file containing an assembly that defines the requested type.

typeName
String String String String

The name of the requested type.

hashValue
Byte[]

Represents the value of the computed hash code.

hashAlgorithm
AssemblyHashAlgorithm AssemblyHashAlgorithm AssemblyHashAlgorithm AssemblyHashAlgorithm

Represents the hash algorithm used by the assembly manifest.

Returns

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.

Exceptions

typeName is an abstract class.

-or-

This member was invoked with a late-binding mechanism.

The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.

An assembly or module was loaded twice with two different evidences.

Remarks

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.

A System.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.

Security

FileIOPermissionAccess
for the ability to read the file containing the assembly manifest. Associated enumeration: Read

WebPermission
for the ability to access the location of the assembly if the assembly is not local.

See also

Applies to