ExtensibleClassFactory ExtensibleClassFactory ExtensibleClassFactory ExtensibleClassFactory Class

定义

在创建过程中启用从非托管对象扩展的托管对象的自定义。Enables customization of managed objects that extend from unmanaged objects during creation.

public ref class ExtensibleClassFactory sealed
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ExtensibleClassFactory
type ExtensibleClassFactory = class
Public NotInheritable Class ExtensibleClassFactory
继承
ExtensibleClassFactoryExtensibleClassFactoryExtensibleClassFactoryExtensibleClassFactory
属性

示例

注册delegate,将从非托管类型的托管类型的实例需要分配聚合的非托管的对象时调用。Registers a delegate that will be called whenever an instance of a managed type that extends from an unmanaged type needs to allocate the aggregated unmanaged object. delegate预期分配和聚合的非托管的对象,并且调用来代替CoCreateInstanceThis delegate is expected to allocate and aggregate the unmanaged object and is called in place of a CoCreateInstance. 此例程必须调用的上下文中static初始值设定项将为其进行回调的类。This routine must be called in the context of the static initializer for the class for which the callbacks will be made.

public ref class CallBack
{
public:
   IntPtr Activate( IntPtr Aggregator )
   {
      ECFSRV32Lib::ObjectActivator^ oCOM = gcnew ECFSRV32Lib::ObjectActivator;
      ECFSRV32Lib::IObjectActivator^ itf = dynamic_cast<ECFSRV32Lib::IObjectActivator^>(oCOM);
      return (IntPtr)itf->CreateBaseComponent( (int)Aggregator );
   }
};

//
// The EcfInner class. First .NET class derived directly from COM class.
//
public ref class EcfInner: public ECFSRV32Lib::BaseComponent
{
private:
   static CallBack^ callbackInner;
   static void RegisterInner()
   {
      callbackInner = gcnew CallBack;
      System::Runtime::InteropServices::ExtensibleClassFactory::RegisterObjectCreationCallback( gcnew System::Runtime::InteropServices::ObjectCreationDelegate( callbackInner, &CallBack::Activate ) );
   }

   //This is the static initializer.    
   static EcfInner()
   {
      RegisterInner();
   }
};
using System;
using System.Runtime.InteropServices;

public class CallBack
{
   public IntPtr Activate(IntPtr Aggregator)
   {
      ECFSRV32Lib.ObjectActivator oCOM = new ECFSRV32Lib.ObjectActivator();
      ECFSRV32Lib.IObjectActivator itf = (ECFSRV32Lib.IObjectActivator)oCOM;
      return (IntPtr) itf.CreateBaseComponent((int)Aggregator);
   }
}

//
// The EcfInner class. First .NET class derived directly from COM class.
//
public class EcfInner : ECFSRV32Lib.BaseComponent
{
   static CallBack callbackInner;

   static void RegisterInner()
   {      
      callbackInner = new CallBack();
      System.Runtime.InteropServices.ExtensibleClassFactory.RegisterObjectCreationCallback(new System.Runtime.InteropServices.ObjectCreationDelegate(callbackInner.Activate));
   }

   //This is the static initializer.    
   static EcfInner()
   {
      RegisterInner();
   }
}
Imports System
Imports System.Runtime.InteropServices

Public Class CallBack
    
    Public Function Activate(Aggregator As IntPtr) As IntPtr
        Dim oCOM As New ECFSRV32Lib.ObjectActivator()
        Dim itf As ECFSRV32Lib.IObjectActivator = _
           CType(oCOM, ECFSRV32Lib.IObjectActivator)
        Return New IntPtr(itf.CreateBaseComponent(Aggregator.ToInt32()))
    End Function
End Class

'
' The EcfInner class. First .NET class derived directly from COM class.
'
Public Class EcfInner
    Inherits ECFSRV32Lib.BaseComponent
    Private Shared callbackInner As CallBack    
    
    Shared Sub RegisterInner()
        callbackInner = New CallBack()
        ExtensibleClassFactory.RegisterObjectCreationCallback( _
           New System.Runtime.InteropServices.ObjectCreationDelegate( _
           AddressOf callbackInner.Activate))
    End Sub    
    
    'This is the static initializer.    
    Shared Sub New()
        RegisterInner()
    End Sub
End Class

注解

ExtensibleClassFactory允许用户指定delegate提供基础的 COM 对象的实例的运行时可调用包装 (RCW) 的构造期间调用。The ExtensibleClassFactory allows users to specify a delegate that is called during construction of a runtime callable wrapper (RCW) that provides an instance of the underlying COM object. 实际上,回调充当包装 RCW 的 COM 对象的类工厂。In effect, the callback acts as the class factory for the COM object wrapped by the RCW. 不执行回调,公共语言运行时创建的基础 COM 对象通过调用CoCreateInstanceWithout the callback, the common language runtime creates the underlying COM object by calling CoCreateInstance. 此回调提供了与 COM 名字对象或通过提供单一实例对象,如激活基础对象的替代方法。This callback provides an alternative way of activating the underlying object, such as with a COM moniker or by providing a singleton object. RegisterObjectCreationCallback必须调用方法static扩展 RCW 类的初始值设定项。The RegisterObjectCreationCallback method must be called in the static initializer of the class that is extending the RCW. 只有一个对象创建回调允许每个对象类型。Only one object creation callback is permitted per object type. 当激活可扩展 RCW 时,注册回调。When the extensible RCW is activated, the callback is registered. 当需要创建基础 COM 对象时,被调用回调来提供对对象的引用。When the underlying COM object needs to be created, the callback is called to provide a reference to the object. 回调必须返回IUnknown基对象的接口指针。The callback must return an IUnknown interface pointer for the base object.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
RegisterObjectCreationCallback(ObjectCreationDelegate) RegisterObjectCreationCallback(ObjectCreationDelegate) RegisterObjectCreationCallback(ObjectCreationDelegate) RegisterObjectCreationCallback(ObjectCreationDelegate)

注册一个 delegate,每次从非托管类型扩展的托管类型的实例需要分配聚合的非托管对象时,都要调用该委托。Registers a delegate that is called when an instance of a managed type, that extends from an unmanaged type, needs to allocate the aggregated unmanaged object.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于