Прочетете на английски Редактиране

Споделяне чрез


ExtensibleClassFactory Class

Definition

Enables customization of managed objects that extend from unmanaged objects during creation.

C#
public sealed class ExtensibleClassFactory
C#
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ExtensibleClassFactory
Inheritance
ExtensibleClassFactory
Attributes

Examples

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. This delegate is expected to allocate and aggregate the unmanaged object and is called in place of a CoCreateInstance. This routine must be called in the context of the static initializer for the class for which the callbacks will be made.

C#
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();
   }
}

Remarks

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. In effect, the callback acts as the class factory for the COM object wrapped by the RCW. Without the callback, the common language runtime creates the underlying COM object by calling CoCreateInstance. This callback provides an alternative way of activating the underlying object, such as with a COM moniker or by providing a singleton object. 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. When the extensible RCW is activated, the callback is registered. When the underlying COM object needs to be created, the callback is called to provide a reference to the object. The callback must return an IUnknown interface pointer for the base object.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
RegisterObjectCreationCallback(ObjectCreationDelegate)

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()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Продукт Версии
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1