ChangeType(object, Type, IFormatProvider)
public static object ChangeType(object value, Type conversionType, IFormatProvider provider)
{
//a few null checks...
IConvertible convertible = value as IConvertible;
if (convertible == null)
{
if (value.GetType() != conversionType)
{
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_IConvertible"));
}
return value;
}
所以它看起来像是一个
不
实施
IConvertible
但已经
目标类型只返回原始对象。
当然这看起来像是
只有
I可转换
,但它是一个例外,看起来像是参数的原因
object
相反。
下面是这个案例的快速LinqPad测试:
void Main()
{
var t = new Test();
var u = Convert.ChangeType(t, typeof(Test));
(u is IConvertible).Dump(); //false, for demonstration only
u.Dump(); //dump of a value Test object
}
public class Test {
public string Bob;
}