Use TInvokeableVariantType to define new data types for Delphi Variants with properties and methods. TInvokeableVariantType descendants do not represent the Variants themselves. Rather, they act as utility classes that the custom Variants call on to perform their operations.
Do not define TInvokeableVariantType descendants in C++. The logic for custom System::Variant
types must be defined in Delphi code.
To create a System::Variant
type that supports properties and methods,
- Map the storage of the System::Variant's data on to the TVarData record.
- Write a method that creates instances of your custom System::Variant. This fills in the System::Variant's data as defined in step 1.
- Declare a class that descends from TInvokeableVariantType.
- Implement all required behavior (including type conversion rules) in the new class. This involves overriding the appropriate virtual methods (typically DoFunction, DoProcedure, GetProperty, SetProperty, BinaryOp, UnaryOp, CompareOp or Compare, Cast, CastTo, Clear, Copy, IsClear, LeftPromotion, and RightPromotion).
- In the initialization section of the unit that defines your TInvokeableVariantType descendant, create an instance of your class. When you instantiate your object, it automatically registers itself with the System::Variant-handling system so that the new System::Variant type is enabled.
- In the finalization section of the unit that defines your TInvokeableVariantType descendant, free the instance of your class. This automatically unregisters the System::Variant type.
For an example, see the VarConv unit, which implements a custom System::Variant type for measurements.
If the custom System::Variant
type uses an object instance to store its data, you can automatically implement the published properties of that object by deriving your custom System::Variant
type from TPublishableVariantType instead.