Check if type TA can cast to type TB at runtime? (Considering something more than inheritance)

孤街醉人 提交于 2020-01-13 11:30:09

问题


So I know that this works:

class A
{
}
class B : A
{       
}

[Test]
public void CanCast()
{
    Assert.That(typeof(A).IsAssignableFrom(typeof(B)));
    Assert.That(!typeof(B).IsAssignableFrom(typeof(A)));
}

However, let's say those two types were Int32 and Int64.

At runtime, I can cast an Int32 value to an Int64 variable, but not the other way around. How can I check this kind of casting-compatibility at runtime? (IsAssignableFrom doesn't work for this, it always gives false for Int32 and Int64)

EDIT: I cannot simply try to cast, because I don't have any value of those types, I'm asking the hypothetical scenario of having two types A and B, not having two values a and b.


回答1:


For non-primitive types, you can reflect and inspect whether there is an op_Implicit method on either type supporting the conversion. IL doesn't actually support true operator overloading, so it's purely a convention system for C# to recognize the operator overloads. The method will also be marked IsSpecialName if it was created from an operator overload definition in C#.

For primitive types (like Int32 and Int64), the simplest option is to hardcode the various cases, as conversion is via a primitive IL opcode rather than via a method. There are only a handful of primitive types, though, so it wouldn't be difficult to create a method with the check for all possibilities for each primitive type.

One side note since your example mentioned primitive value types specifically, note that the existence of an implicit 'conversion' (in C#) does not mean that all 'casts' will work. The C# cast operation (T)x can also mean 'unbox the value in x to type T'. If x contains a boxed Int32 and you attempt to (Int64)x, this will fail at runtime, even though you can implicitly 'convert' an Int32 to an Int64. See Eric Lippert for more information as to why unboxing works this way.




回答2:


One (less-than-elegant) approach is to simply try it - wrap your attempt in a try/catch and Assert false if you catch an exception.



来源:https://stackoverflow.com/questions/12749364/check-if-type-ta-can-cast-to-type-tb-at-runtime-considering-something-more-tha

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!