I think you misunderstand the essence of the operator. The as operator is roughly equivalent to the following code:
if (firstObject is SomeType) return (SomeType)firstObject; else return null;
Since this is more of an inheritance check. (For example, List implements IList)
Value types do not support inheritance and not without reason. Double and Int64 both store number 1 in completely different ways.
Basically, you want to use a method that will determine for you whether the number conversion is hassle-free or not. Well, I agree with "Why?". Although quite a few formats are supported in the CLR, conversion rules are usually quite simple. For example, Int32 β Double is lossless, and any conversion from "smaller" to "larger" is lossless, for example SByte β Int64.
Another question: what will the false mean in your example? I would say very little, for example:
Convert.CanConvert(123456789.12345F, typeof(Byte))
What use is a false result? You mean that this applies to cases like Int32 -> Single, where some data will be lost, but in this case a ton of data is lost, since the "nearest" byte representation is 255.
It is precisely because of these two problems that there is no such method.
Guvante
source share