Class ComSafeArray<T>
- Namespace
- CsCom.Runtime
- Assembly
- CsCom.Runtime.dll
A SAFEARRAY of any rank whose elements are accessed as T. Unlike the .NET arrays converted
from SAFEARRAYs, it keeps the bounds of the native array, e.g. 1-based arrays created by Visual Basic or Excel.
[NativeMarshalling(typeof(ComSafeArrayMarshaller<>))]
public sealed class ComSafeArray<T> : IDisposable
Type Parameters
TThe type of the elements.
- Inheritance
-
ComSafeArray<T>
- Implements
- Inherited Members
Remarks
An instance that owns its SAFEARRAY destroys it when it is disposed or finalized; a view of a SAFEARRAY
owned by someone else does not. When it is passed to COM methods, the ownership follows the COM rules: [in]
arrays stay owned by the caller, returned arrays are owned by the receiver, and an array replaced by the callee of an
[in, out] parameter is detached. The arrays received by COM objects implemented in .NET are views that are valid
during the call; an owned array returned by them is detached and its ownership is transferred to the caller.
The elements are converted to and from T on each access. The indices follow the order of
the SAFEARRAY indices, e.g. (row, column) for Excel ranges, and the dimensions are numbered from 0.
Properties
ElementType
Gets the variant type of the elements.
public VarEnum ElementType { get; }
Property Value
- VarEnum
this[int]
Gets or sets an element of a one-dimensional array.
public T this[int index] { get; set; }
Parameters
indexintThe index, within the bounds of the array.
Property Value
- T
this[int, int]
Gets or sets an element of a two-dimensional array.
public T this[int index0, int index1] { get; set; }
Parameters
index0intThe index of the first dimension, within its bounds.
index1intThe index of the second dimension, within its bounds.
Property Value
- T
this[int, int, int]
Gets or sets an element of a three-dimensional array.
public T this[int index0, int index1, int index2] { get; set; }
Parameters
index0intThe index of the first dimension, within its bounds.
index1intThe index of the second dimension, within its bounds.
index2intThe index of the third dimension, within its bounds.
Property Value
- T
this[ReadOnlySpan<int>]
Gets or sets an element of a multidimensional array.
public T this[params ReadOnlySpan<int> indices] { get; set; }
Parameters
indicesReadOnlySpan<int>The indices of the dimensions, within their bounds.
Property Value
- T
Length
Gets the total number of elements.
public int Length { get; }
Property Value
OwnsArray
Gets a value indicating whether the instance destroys the SAFEARRAY when it is disposed.
public bool OwnsArray { get; }
Property Value
Pointer
Gets the SAFEARRAY pointer.
public nint Pointer { get; }
Property Value
Exceptions
- ObjectDisposedException
The instance was disposed or detached.
Rank
Gets the number of dimensions.
public int Rank { get; }
Property Value
Methods
AsSpan()
Gets the elements of a one-dimensional array in the memory of the SAFEARRAY, without converting them.
public Span<T> AsSpan()
Returns
- Span<T>
Remarks
The element at index 0 of the span is at the lower bound of the array. T must have the layout of
the elements: the numeric type of the elements or an enum based on it, decimal for VT_DECIMAL,
ComBool for VT_BOOL, ComDate for VT_DATE or ComCurrency for
VT_CY. The span is valid until the instance is disposed or detached, and while the SAFEARRAY is not
resized or destroyed by its owner. An instance that owns its SAFEARRAY destroys it when it is finalized, so
the instance must be kept reachable while the span is used, e.g. by disposing it afterwards.
Exceptions
- InvalidOperationException
The array is not one-dimensional, or its elements cannot be accessed as
T.- ObjectDisposedException
The instance was disposed or detached.
Create(VarEnum, ReadOnlySpan<int>, ReadOnlySpan<int>)
Creates a SAFEARRAY whose elements are zero, empty or null.
public static ComSafeArray<T> Create(VarEnum elementType, ReadOnlySpan<int> lengths, ReadOnlySpan<int> lowerBounds = default)
Parameters
elementTypeVarEnumThe variant type of the elements, e.g.
VT_UNKNOWNfor object elements that are interface pointers.lengthsReadOnlySpan<int>The number of elements of each dimension.
lowerBoundsReadOnlySpan<int>The lower bounds of the dimensions; zero when empty.
Returns
- ComSafeArray<T>
Create(T[,,])
Creates a three-dimensional SAFEARRAY with the bounds and the elements of an array; the element type is derived from T.
public static ComSafeArray<T> Create(T[,,] values)
Parameters
valuesT[,,]
Returns
- ComSafeArray<T>
Create(T[,])
Creates a two-dimensional SAFEARRAY with the bounds and the elements of an array; the element type is derived from T.
public static ComSafeArray<T> Create(T[,] values)
Parameters
valuesT[,]
Returns
- ComSafeArray<T>
Create(T[])
Creates a one-dimensional SAFEARRAY with the elements of an array; the element type is derived from T.
public static ComSafeArray<T> Create(T[] values)
Parameters
valuesT[]
Returns
- ComSafeArray<T>
Detach()
Gives up the SAFEARRAY without destroying it, e.g. to return it to a COM caller. The instance cannot be used afterwards.
public nint Detach()
Returns
- nint
The
SAFEARRAYpointer, which is owned by the caller if the instance owned it.
Dispose()
Destroys the SAFEARRAY if it is owned. The instance cannot be used afterwards.
public void Dispose()
~ComSafeArray()
Destroys the SAFEARRAY if it is owned and was not disposed.
protected ~ComSafeArray()
FromPointer(nint, bool)
Wraps a SAFEARRAY pointer, e.g. a SAFEARRAY field of a structure.
public static ComSafeArray<T>? FromPointer(nint pointer, bool ownsArray)
Parameters
pointernintThe
SAFEARRAYpointer.ownsArrayboolWhether the instance destroys the
SAFEARRAYwhen it is disposed.
Returns
- ComSafeArray<T>
The wrapper, or null if
pointeris 0.
GetLength(int)
Gets the number of elements of a dimension.
public int GetLength(int dimension)
Parameters
dimensionintThe zero-based dimension.
Returns
GetLowerBound(int)
Gets the lower bound of a dimension.
public int GetLowerBound(int dimension)
Parameters
dimensionintThe zero-based dimension.
Returns
GetUpperBound(int)
Gets the upper bound of a dimension.
public int GetUpperBound(int dimension)
Parameters
dimensionintThe zero-based dimension.
Returns
GetValue(params ReadOnlySpan<int>)
Gets an element.
public T GetValue(params ReadOnlySpan<int> indices)
Parameters
indicesReadOnlySpan<int>The index of each dimension, within its bounds.
Returns
- T
Exceptions
- IndexOutOfRangeException
An index is outside of the bounds.
SetValue(T, params ReadOnlySpan<int>)
Sets an element, converting the value to the element type of the SAFEARRAY.
public void SetValue(T value, params ReadOnlySpan<int> indices)
Parameters
valueTThe new value.
indicesReadOnlySpan<int>The index of each dimension, within its bounds.
Exceptions
- IndexOutOfRangeException
An index is outside of the bounds.
ToArray()
Copies the elements of a one-dimensional array to a zero-based .NET array.
public T[] ToArray()
Returns
- T[]
Exceptions
- InvalidOperationException
The array is not one-dimensional.
ToArray2D()
Copies the elements of a two-dimensional array to a zero-based .NET array.
public T[,] ToArray2D()
Returns
- T[,]
Exceptions
- InvalidOperationException
The array is not two-dimensional.
ToArray3D()
Copies the elements of a three-dimensional array to a zero-based .NET array.
public T[,,] ToArray3D()
Returns
- T[,,]
Exceptions
- InvalidOperationException
The array is not three-dimensional.