LifetimeStrategyOverride
The LifetimeStrategyOverride
aspect can be applied on assembly level to override the Life-Time Strategy used for a specific type.
This is useful for types that require memory management not handled by the default garbage collector, such as types referring to data that must be explicitly allocated and deallocated using different APIs, such as for example the rtl.winrt.HString
type on the Windows platform. A custom life-time strategy allows to to be treated more like a regular String type.
The attribute must be specified on assembly (project) level, and requires two parameters:
[assembly: LifetimeStrategyOverride(typeOf(rtl.winrt.HSTRING), typeOf(HSTRING_Helper))]
[assembly: LifetimeStrategyOverride(typeOf(rtl.winrt.HSTRING), typeOf(HSTRING_Helper))]
@__assembly: LifetimeStrategyOverride(typeOf(rtl.winrt.HSTRING), typeOf(HSTRING_Helper))
@__assembly: LifetimeStrategyOverride(typeOf(rtl.winrt.HSTRING), typeOf(HSTRING_Helper))
<Assembly:LifetimeStrategyOverride(typeOf(rtl.winrt.HSTRING), typeOf(HSTRING_Helper))>
The first parameter specifies the type for which to override the stragtegy, in this case rtl.winrt.HSTRING
. The second is the helper type that implements the special behavior.
The helper must be a record
/struct
with a single field of the target type. This helper must define operators such as copy
, assign
, a copy constructor and a finalizer.
Please refer to the HSTRING_Helper
class in the Island RTL source code on GitHub as a reference, should you need to implement your own.
Island Only
The LifetimeStrategyOverride
aspect is available on the Island-backed platform only.