简体   繁体   English

是否Convert.ChangeType快捷方式?

[英]Does Convert.ChangeType Shortcut?

I'm wondering what happens inside Object Convert.ChangeType( Object object, Type type) when the type of object matches type . 我想知道当object typetype相匹配时Object Convert.ChangeType( Object object, Type type)内部会发生什么。 Does it just short-cut and return object ? 它只是捷径并返回object吗?

For example: 例如:

object objString = "Hello World";

string converted = (string) Convert.ChangeType( objString, typeof ( String ) );

Would the implementation of ChangeType actually call the IConvertible interface or just return objString? ChangeType的实现会实际调用IConvertible接口还是只返回objString?

According to Microsoft's C# reference source , Convert.ChangeType(Object, Type) performs the following general behaviour: 根据Microsoft的C#参考源Convert.ChangeType(Object, Type)执行以下一般行为:

  • If the input is null 如果输入为空
    • If the type is a value type, throw. 如果类型是值类型,则抛出。
    • Otherwise return null. 否则返回null。
  • If the input is not IConvertible 如果输入不是IConvertible
    • If the type is exactly the input's type, return the input. 如果类型恰好是输入的类型,则返回输入。
    • Otherwise throw. 否则抛出。
  • If the type is one of the out-of-the-box core convertible types, call the corresponding ToWhatever method on the input. 如果该类型是现成的核心可转换类型之一,请在输入上调用相应的ToWhatever方法。
    • In each case, the implementation seems to be return this if the types match or return Convert.ToWhatever(this) otherwise, which is a shortcut of sorts. 在每种情况下,如果类型匹配,则实现似乎return this ,否则return Convert.ToWhatever(this) ,这是一种排序的快捷方式。
  • Otherwise call ToType on the input, passing the type through. 否则,在输入上调用ToType ,以传递类型。

Yes, it would call IConvertible interface on the object. 是的,它将在对象上调用IConvertible接口。 In case of string it would call objString.ToString() which in turn would return itself (return this). 如果是字符串,它将调用objString.ToString(),而objString.ToString()随后将返回自身(返回此值)。

In addition, if type of object does not implement IConvertible and you convert to the same type then it will just return the same object. 另外,如果对象类型未实现IConvertible,而您转换为相同类型,则它将仅返回相同对象。

If type does not implement IConvertible and you convert to different type then exception will be thrown. 如果type不实现IConvertible,而您将其转换为其他类型,则将引发异常。

This MSDN blog article might help you. 此MSDN博客文章可能会为您提供帮助。

If the class implements IConvertable interface, you can then use System.Convert.ChangeType to change the data type: 如果该类实现IConvertable接口,则可以使用System.Convert.ChangeType更改数据类型:

decimal x = (decimal) System.Convert.ChangeType("5", typeof(decimal));

Think of ChangeType as a big switch statement (select case in VB)… with lots of overloaded functions. 可以将ChangeType视为一个很大的switch语句(在VB中选择大小写)…具有许多重载函数。 Something like this (disclaimer: this is intended to be a pseudo-code, not the exact .NET's implementation): 这样的事情(免责声明:这旨在成为伪代码,而不是确切的.NET实现):

public static Object ChangeType(Object value, TypeCode typeCode , IFormatProvider provider)
{

IConvertible v = value as IConvertible;

switch (typeCode) {

case TypeCode.Boolean:
    return v.ToBoolean(provider);

case TypeCode.Char:
    return v.ToChar(provider);

case TypeCode.SByte:
    return v.ToSByte(provider);

case TypeCode.Byte:
    return v.ToByte(provider);

case TypeCode.Int16:
    return v.ToInt16(provider);

case TypeCode.UInt16:
    return v.ToUInt16(provider);

. . .

}

A class that claims to implement IConvertible interface must implement all the conversions in the switch structure above plus GetTypeCode: 声称要实现IConvertible接口的类必须在上面的开关结构中加上GetTypeCode来实现所有转换:

· GetTypeCode ·GetTypeCode

· ToBoolean ·布尔值

· ToByte ·ToByte

· ToChar ·托查

· ToDateTime ·ToDateTime

· ToDecimal ·十进制

· ToDouble ·ToDouble

· ToInt16 ·ToInt16

· ToInt32 ·ToInt32

· ToInt64 ·ToInt64

· ToSByte ·ToSByte

· ToSingle ·ToSingle

· ToString ·ToString

· ToType · 输入

· ToUInt16 ·ToUInt16

· ToUInt32 ·ToUInt32

· ToUInt64 ·ToUInt64

System.Convert class has a number of implementations that can be, in turn, called by the class implementing IConvertible interface. System.Convert类具有许多实现,而这些实现又可以由实现IConvertible接口的类调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM