小编Mic*_* T.的帖子

TypeDescriptor CanConvertFrom Bug?或者我做错了?

这是从http://dnpextensions.codeplex.com/中取出的扩展方法.

我理解字符串"test"不是数字字符串......

我知道GetConverter(targetType)的类型是int ...

我不明白为什么它说它可以从一个字符串转换...但它失败了...

/// <summary>
///     Converts an object to the specified target type or returns the default value.
/// </summary>
/// <typeparam name = "T"></typeparam>
/// <param name = "value">The value.</param>
/// <param name = "defaultValue">The default value.</param>
/// <returns>The target type</returns>
public static T ConvertTo<T>(this object value, T defaultValue)
{
    if (value != null)
    {
        var targetType = typeof(T);
        var valueType = value.GetType();

        if (valueType == targetType) return (T)value;

        var converter = TypeDescriptor.GetConverter(value);
        if …
Run Code Online (Sandbox Code Playgroud)

c# system

1
推荐指数
1
解决办法
1423
查看次数

标签 统计

c# ×1

system ×1