संपादित करें

इसके माध्यम से साझा किया गया


AppDomain.CreateComInstanceFrom Method

Definition

Creates a new instance of a specified COM type.

Overloads

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)

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)

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

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

typeName
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

assemblyName or typeName is null.

The type cannot be loaded.

The operation is attempted on an unloaded application domain.

No public parameterless constructor was found.

assemblyName is not found.

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.

assemblyName is an empty string ("").

assemblyName is not a valid assembly.

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

The COM object that is being referred to is null.

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 CreateComInstanceFromSnippet {
   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);
      }
   }
}
open System
open System.Runtime.InteropServices

[<ComVisible true>]
type MyComVisibleType() =
    do
        printfn "MyComVisibleType instantiated!"

[<ComVisible false>]
type MyComNonVisibleType() =
    do
        printfn "MyComNonVisibleType instantiated!"

let createComInstance typeName =
    try
        let currentDomain = AppDomain.CurrentDomain
        let assemblyName = currentDomain.FriendlyName
        currentDomain.CreateComInstanceFrom(assemblyName, typeName)
        |> ignore
    with e ->
        printfn $"{e.Message}"

createComInstance "MyComNonVisibleType"   // Fail!
createComInstance "MyComVisibleType"      // OK!
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.

See also

Applies to

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

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

typeName
String

The name of the requested type.

hashValue
Byte[]

Represents the value of the computed hash code.

hashAlgorithm
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

assemblyFile or typeName is null.

The type cannot be loaded.

The operation is attempted on an unloaded application domain.

No public parameterless constructor was found.

assemblyFile is not found.

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.

assemblyFile is the empty string ("").

assemblyFile is not a valid assembly.

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

The COM object that is being referred to is null.

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.

See also

Applies to