Updated C# and .NET docs supplement (markdown)
parent
088e6eb7f7
commit
a702b1dcdd
|
@ -66,7 +66,53 @@ You can only have 1 `default:` branch (not to be confused with `case default:`),
|
|||
|
||||
## `MemoryMarshal.Cast` can't be used with reference types
|
||||
|
||||
See [this SO answer](https://stackoverflow.com/a/79146967/7467292). tl;dr: It's possible in .NET Core only.
|
||||
See [this SO answer](https://stackoverflow.com/a/79146967). tl;dr: It's possible in .NET Core only.
|
||||
|
||||
<details>
|
||||
<summary>full implementations of extension methods for later</summary>
|
||||
|
||||
```csharp
|
||||
public static Span<TTo> Cast<TFrom, TTo>(Span<TFrom> span)
|
||||
where TTo: class
|
||||
where TFrom: class
|
||||
{
|
||||
if (typeof(TTo).IsAssignableFrom(typeof(TFrom))) throw new InvalidCastException();
|
||||
return UnsafeDowncast<TFrom, TTo>(span);
|
||||
}
|
||||
|
||||
public static ReadOnlySpan<TTo> Cast<TFrom, TTo>(ReadOnlySpan<TFrom> span)
|
||||
where TTo: class
|
||||
where TFrom: class
|
||||
{
|
||||
if (typeof(TTo).IsAssignableFrom(typeof(TFrom))) throw new InvalidCastException();
|
||||
return UnsafeDowncast<TFrom, TTo>(span);
|
||||
}
|
||||
|
||||
public static Span<TBase> CastUp<TDerived, TBase>(Span<TDerived> span)
|
||||
where TBase: class
|
||||
where TDerived: class, TBase
|
||||
=> UnsafeDowncast<TDerived, TBase>(span);
|
||||
|
||||
public static ReadOnlySpan<TBase> CastUp<TDerived, TBase>(ReadOnlySpan<TDerived> span)
|
||||
where TBase: class
|
||||
where TDerived: class, TBase
|
||||
=> UnsafeDowncast<TDerived, TBase>(span);
|
||||
|
||||
public static Span<TTo> UnsafeDowncast<TFrom, TTo>(Span<TFrom> span)
|
||||
where TTo: class
|
||||
where TFrom: class
|
||||
=> MemoryMarshal.CreateSpan<TTo>(
|
||||
ref Unsafe.As<TFrom, TTo>(ref MemoryMarshal.GetReference(span)),
|
||||
span.Length);
|
||||
|
||||
public static ReadOnlySpan<TTo> UnsafeDowncast<TFrom, TTo>(ReadOnlySpan<TFrom> span)
|
||||
where TTo: class
|
||||
where TFrom: class
|
||||
=> MemoryMarshal.CreateReadOnlySpan<TTo>(
|
||||
ref Unsafe.As<TFrom, TTo>(ref MemoryMarshal.GetReference(span)),
|
||||
span.Length);
|
||||
```
|
||||
</details>
|
||||
|
||||
## MSBuild `Condition` placement
|
||||
|
||||
|
|
Loading…
Reference in New Issue