I am trying to create a class (
Measurement) that should mimic the internal C# numeric classes (
int,
double,
float, etc) as closely as possible. To this end I have defined the
Measurement class to have its own implementations of the methods used by the C# classes.
However, I am having trouble accessing the new implementions. The code example below shows the syntax I am using.
My class includes the following in order to define the
TryConvertFromChecked() method:
public partial class Measurement<T> : ..., INumberBase<Measurement<T>>,...
where T : ...,INumberBase<T>, ...
{
...
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase<Measurement<T>>.TryConvertFromChecked<TOther>(TOther value, [MaybeNullWhen(false)] out Measurement<T> result)
{
return TryConvertFrom(value, out result, ConversionOption.Checked);
}
private static bool TryConvertFrom<TOther>([NotNull] TOther value, [MaybeNullWhen(false)] out Measurement<T> result, ConversionOption option)
where TOther : INumberBase<TOther>
{
...
}
...
}
And I am trying to call that method as follows:
if (!T.TryConvertFromChecked(toUnit.CompoundFactor, out var toCompoundFactor))
{
throw new ApplicationException($"An internal error occurred while converting the CompoundFactor for [{unitSymbol}] from type [{toUnit.CompoundFactor}] to type [{typeof(T)}]");
}
But this is always executing the C# internal
System.Numerics.INumberBase<tself> implementation. How would I get my call to
TryConvertFromChecked() to call the one that I have created?
What I have tried:
if (!T.Measurement<T>.TryConvertFromChecked(toUnit.CompoundFactor, out var toCompoundFactor))
Results in
CS0704 Cannot do non-virtual member lookup in 'T' because it is a type parameter