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 (converter != null)
{
if (converter.CanConvertTo(targetType))
return (T)converter.ConvertTo(value, targetType);
}
converter = TypeDescriptor.GetConverter(targetType);
if (converter != null)
{
if (converter.CanConvertFrom(valueType))
return (T)converter.ConvertFrom(value);
}
}
return defaultValue;
}
[TestMethod]
public void TestConvertToWillFail()
{
// Arrange
var value = "test";
// Act
var result = value.ConvertTo<int>();
// Assert
result.Should().Equal(0);
result.Should().Not.Equal(value);
}
[TestMethod]
public void TestConvertToShouldPass()
{
// Arrange
var value = 123;
var stringValue = "123";
// Act
var stringResult = stringValue.ConvertTo<int>();
// Assert
stringResult.Should().Equal(value);
stringResult.Should().Not.Equal(0);
}
注意: Should() 来自 Should.codeplex.com
测试异常:
test 不是 Int32 的有效值。
This is an extension method taken out from http://dnpextensions.codeplex.com/.
I understand that the string "test" isn't a number string...
I understand that the GetConverter(targetType) is of type int...
What I don't understand is why it say it can convert from a string... but it fail...
/// <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 (converter != null)
{
if (converter.CanConvertTo(targetType))
return (T)converter.ConvertTo(value, targetType);
}
converter = TypeDescriptor.GetConverter(targetType);
if (converter != null)
{
if (converter.CanConvertFrom(valueType))
return (T)converter.ConvertFrom(value);
}
}
return defaultValue;
}
[TestMethod]
public void TestConvertToWillFail()
{
// Arrange
var value = "test";
// Act
var result = value.ConvertTo<int>();
// Assert
result.Should().Equal(0);
result.Should().Not.Equal(value);
}
[TestMethod]
public void TestConvertToShouldPass()
{
// Arrange
var value = 123;
var stringValue = "123";
// Act
var stringResult = stringValue.ConvertTo<int>();
// Assert
stringResult.Should().Equal(value);
stringResult.Should().Not.Equal(0);
}
Note: Should() is from Should.codeplex.com
Exception from test:
test is not a valid value for Int32.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的方法在第二次调用中执行的操作是:
CanConvertFrom/To
方法只是验证调用是否有意义,而不是验证转换是否会成功,因为CanConvert
仅适用于类型级别。将转换为有效整数,但这并不意味着所有字符串都是有效整数,因此即使
CanConvert
返回 true,ConvertFrom/To
也会抛出异常。What your method is doing in the second call is:
The
CanConvertFrom/To
methods are just to verify if the call makes sense at all, not whether the conversion will succeed, sinceCanConvert
works only on the type levelThere are strings that will convert to valid integers, but that does not mean that all strings are valid integers, so
ConvertFrom/To
will throw exceptions, even if theCanConvert
return true.这是我的工作。
请让我知道是否有更好的版本。
This is my work around.
Please let me know if there's a better version of this out there.
如果无法执行转换,ConvertTo 可能会引发异常。即使 CanConvertTo 返回 true。
例如,“12”可以转换为整数,但“test”不能。但是转换器会说它可以从字符串转换为整数。
ConvertTo can throw exceptions if it is not able to perform the conversion... even if CanConvertTo returns true.
e.g. "12" can be converted to an integer but "test" cannot be. However the converter would say that it can convert from string to integer.