Struct Variant
Represents the variant value on the stack.
Implements
Inherited Members
Namespace: DotNext.Runtime
Assembly: DotNext.dll
Syntax
public readonly ref struct Variant : IEquatable<Variant>
Remarks
This type is a counterpart of TypedReference.
Constructors
View SourceVariant(TypedReference)
Converts TypedReference to the variant value.
Declaration
[CLSCompliant(false)]
public Variant(TypedReference typedRef)
Parameters
| Type | Name | Description |
|---|---|---|
| TypedReference | typedRef | The typed reference to convert. |
Properties
View SourceEmpty
Declaration
public static Variant Empty { get; }
Property Value
| Type | Description |
|---|---|
| Variant |
IsEmpty
Gets a value indicating that this value is empty.
Declaration
public bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsMutable
Gets a value indicating that the underlying value can be mutated.
Declaration
public bool IsMutable { get; }
Property Value
| Type | Description |
|---|---|
| bool |
TargetType
Gets the type of the value.
Declaration
public Type TargetType { get; }
Property Value
| Type | Description |
|---|---|
| Type |
Methods
View SourceClear()
Resets the referenced value to the default value of type TargetType.
Declaration
public void Clear()
Remarks
Reference types are set to null, value types are zeroed.
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | The current value is immutable or empty. |
Equals(scoped Variant)
Checks whether this container references the same value and type as other.
Declaration
public bool Equals(scoped Variant other)
Parameters
| Type | Name | Description |
|---|---|---|
| Variant | other | The value to compare. |
Returns
| Type | Description |
|---|---|
| bool | true, if this container references the same value and type as |
Immutable(Variant)
Creates immutable value.
Declaration
public static Variant Immutable(Variant value)
Parameters
| Type | Name | Description |
|---|---|---|
| Variant | value | The location of the value. |
Returns
| Type | Description |
|---|---|
| Variant | The untyped immutable value. |
Immutable<T>()
Gets the read-only reference to the underlying value.
Declaration
public ref readonly T Immutable<T>() where T : allows ref struct
Returns
| Type | Description |
|---|---|
| T | A location of the value. |
Type Parameters
| Name | Description |
|---|---|
| T | The expected type of the value. |
Exceptions
| Type | Condition |
|---|---|
| InvalidCastException | The underlying value cannot be converted to type |
Immutable<T>(ref readonly T)
Creates an immutable value and stores its type.
Declaration
public static Variant Immutable<T>(ref readonly T location) where T : allows ref struct
Parameters
| Type | Name | Description |
|---|---|---|
| T | location | The location of the value. |
Returns
| Type | Description |
|---|---|
| Variant | The untyped value. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the value. |
IsTypeOf<T>()
Determines whether the type of the referenced value is exactly T.
Declaration
public bool IsTypeOf<T>() where T : allows ref struct
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
Unlike the is operator, this method doesn't take inheritance into account, so it returns true only if Immutable<T>() succeeds.
Mutable(ValueType)
Creates mutable boxed value and stores its type.
Declaration
public static Variant Mutable(ValueType boxedValue)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueType | boxedValue | The boxed value. |
Returns
| Type | Description |
|---|---|
| Variant | The boxed value. |
Mutable<T>()
Tries to get the mutable reference to the underlying value.
Declaration
public ref T Mutable<T>() where T : allows ref struct
Returns
| Type | Description |
|---|---|
| T | A location of the value. |
Type Parameters
| Name | Description |
|---|---|
| T | The expected type of the value. |
Exceptions
| Type | Condition |
|---|---|
| InvalidCastException | The underlying value cannot be converted to type |
Mutable<T>(ref T)
Creates mutable value and stores its type.
Declaration
public static Variant Mutable<T>(ref T location) where T : allows ref struct
Parameters
| Type | Name | Description |
|---|---|---|
| T | location | The location of the value. |
Returns
| Type | Description |
|---|---|
| Variant | The untyped value. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the value. |
ToObject()
Tries to extract the underlying value.
Declaration
public object? ToObject()
Returns
| Type | Description |
|---|---|
| object | The underlying value. |
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | The underlying value is by-ref like struct and cannot be boxed. |
ToString()
Declaration
public override string? ToString()
Returns
| Type | Description |
|---|---|
| string |
Overrides
View SourceTryCopyTo(scoped Variant)
Copies the value from the current location to the destination.
Declaration
public bool TryCopyTo(scoped Variant destination)
Parameters
| Type | Name | Description |
|---|---|---|
| Variant | destination | The destination location. |
Returns
| Type | Description |
|---|---|
| bool | true if both memory locations of compatible types; otherwise, false. |
TrySetValue(object?)
Tries to replace the referenced value.
Declaration
public bool TrySetValue(object? value)
Parameters
| Type | Name | Description |
|---|---|---|
| object | value | The value to set. |
Returns
| Type | Description |
|---|---|
| bool | true if the value is replaced successfully;
false if the current value is immutable or empty, or |
Remarks
The accepted value depends on TargetType:
- For a value type, the boxed value of exactly the same type.
- For Nullable<T>, the boxed value of the underlying type, or null.
- For a reference type, any compatible object, or null.
- For an unmanaged pointer type, the boxed pointer as Pointer. The type of the boxed pointer is not checked against TargetType.
- For a function pointer type, the boxed nint.
Pointers are boxed the same way as ToObject() does, including zero pointers. By-ref-like types are not supported.
Operators
View Sourceoperator ==(scoped Variant, scoped Variant)
Determines whether the two containers reference the same value and type.
Declaration
public static bool operator ==(scoped Variant x, scoped Variant y)
Parameters
| Type | Name | Description |
|---|---|---|
| Variant | x | The first container to compare. |
| Variant | y | The second container to compare. |
Returns
| Type | Description |
|---|---|
| bool | true if both containers reference the same value and type; otherwise, false. |
implicit operator Variant(TypedReference)
Converts TypedReference to the variant value.
Declaration
[CLSCompliant(false)]
public static implicit operator Variant(TypedReference typedRef)
Parameters
| Type | Name | Description |
|---|---|---|
| TypedReference | typedRef | The typed reference to convert. |
Returns
| Type | Description |
|---|---|
| Variant | The variant points to the same location as |
operator !=(scoped Variant, scoped Variant)
Determines whether the two containers reference the different values and types.
Declaration
public static bool operator !=(scoped Variant x, scoped Variant y)
Parameters
| Type | Name | Description |
|---|---|---|
| Variant | x | The first container to compare. |
| Variant | y | The second container to compare. |
Returns
| Type | Description |
|---|---|
| bool | true if both containers reference the different values and types; otherwise, false. |