我知道我可以(隐式)将 aint转换为 a float,或将 afloat转换为 a double。
另外,我可以(明确地)将 adouble转换为 afloat或 a int。
这可以通过下面的例子来证明:
int i;
float f;
// The smaller type fits into the bigger one
f = i;
// And the bigger type can be cut (losing precision) into a smaller
i = (int)f;
问题是这种类型不是从另一个继承的(int不是子类型,float反之亦然)。
他们已经实现了隐式/显式转换运算符或类似的东西。如果没有,它就像它一样工作......
我的问题是:如何检查A 类型的变量是否可以转换为 B 类型。
我试过了i.GetType().IsAssignableFrom(f.GetType()),但Type.IsAssignableFrom(Type)只检查继承和接口(也许还有更多),但不检查实现的强制转换运算符。
我试过i is floatand f is int,但效果是一样的。