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.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Function CreateComInstanceFrom ( _
	assemblyName As String, _
	typeName As String _
) As ObjectHandle

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.

ExceptionCondition
ArgumentNullException

assemblyName or typeName is Nothing.

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 Nothing.

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.

The following sample demonstrates


Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices

<ComVisible(True)> _
Class MyComVisibleType

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

End Class 'MyComVisibleType

<ComVisible(False)> _
Class MyComNonVisibleType

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

End Class 'MyComNonVisibleType

Module Test

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

   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 'CreateComInstance

End Module 'Test


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft