Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Convert.ChangeType是否在内部调用ToString()?_C#_.net_Type Conversion - Fatal编程技术网

C# Convert.ChangeType是否在内部调用ToString()?

C# Convert.ChangeType是否在内部调用ToString()?,c#,.net,type-conversion,C#,.net,Type Conversion,以下两者之间有什么区别吗 value.ToString() 及 (string)Convert.ChangeType(value,typeof(string))它在确保类型是可转换的之后调用IConvertable.ToString case TypeCode.String: return (object) convertible.ToString(provider); 因此,只需使用IFormatProvider调用ToString,就可以做更多的工作。这一切都取决于实现i可转换的类型

以下两者之间有什么区别吗

value.ToString()


(string)Convert.ChangeType(value,typeof(string))
它在确保类型是可转换的之后调用
IConvertable.ToString

case TypeCode.String:
   return (object) convertible.ToString(provider);
因此,只需使用
IFormatProvider
调用
ToString
,就可以做更多的工作。这一切都取决于实现
i可转换
的类型的实现

case TypeCode.String:
   return (object) convertible.ToString(provider);
提供程序
来自
(IFormatProvider)Thread.CurrentThread.CurrentCulture

这就是
int
所做的

public override string ToString()
{
  return Number.FormatInt32(this, (string) null, NumberFormatInfo.CurrentInfo);
}

public string ToString(string format)
{
  return Number.FormatInt32(this, format, NumberFormatInfo.CurrentInfo);
}

public string ToString(IFormatProvider provider)
{
  return Number.FormatInt32(this, (string) null, NumberFormatInfo.GetInstance(provider));
}

public string ToString(string format, IFormatProvider provider)
{
  return Number.FormatInt32(this, format, NumberFormatInfo.GetInstance(provider));
}

在确保类型是可转换的之后,它调用
IConvertable.ToString

case TypeCode.String:
   return (object) convertible.ToString(provider);
因此,只需使用
IFormatProvider
调用
ToString
,就可以做更多的工作。这一切都取决于实现
i可转换
的类型的实现

case TypeCode.String:
   return (object) convertible.ToString(provider);
提供程序
来自
(IFormatProvider)Thread.CurrentThread.CurrentCulture

这就是
int
所做的

public override string ToString()
{
  return Number.FormatInt32(this, (string) null, NumberFormatInfo.CurrentInfo);
}

public string ToString(string format)
{
  return Number.FormatInt32(this, format, NumberFormatInfo.CurrentInfo);
}

public string ToString(IFormatProvider provider)
{
  return Number.FormatInt32(this, (string) null, NumberFormatInfo.GetInstance(provider));
}

public string ToString(string format, IFormatProvider provider)
{
  return Number.FormatInt32(this, format, NumberFormatInfo.GetInstance(provider));
}

当目标类型为string时,Convert.ChangeType的工作方式如下:


if (value == null)
{
    return null;
}

var convertible = value as IConvertible;
if (convertible == null)
{
    throw new InvalidCastException();
}

return convertible.ToString();

所以它与value.ToString()完全不同

当目标类型为字符串时,Convert.ChangeType的工作方式如下:


if (value == null)
{
    return null;
}

var convertible = value as IConvertible;
if (convertible == null)
{
    throw new InvalidCastException();
}

return convertible.ToString();

所以它与value.ToString()完全不同

作为将来的参考,请下载dotPeek。作为将来的参考,请下载dotPeek。