'Downcasting generic maths types
Assuming that I have a generic class/method with a generic number constraint (using the new preview generic math feature):
public void DoAThing<TValue>(TValue value) where TValue : struct, INumber<TValue>
{
...
}
Is there a way to downcast this further to one of the other interfaces? For example, one of these sorts of things:
if (typeof(IFloatingPoint<TValue>).IsAssignableFrom(typeof(TValue)))
{
// do some floating-point-only logic without a specific value
}
if (value is IFloatingPoint<TValue> fpnum)
{
// do some floating-point-only logic on fpnum
}
Currently, the compiler objects to this, saying that TValue must be convertible to IFloatingPoint<TValue> to use it as a parameter... which is exactly what I'm trying to test.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
