ConvertFromString不返回默认null [英] ConvertFromString not returning default null
本文介绍了ConvertFromString不返回默认null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么以下代码返回一个空字符串而不是null?
Why does the following code return an empty string and not null?
TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(String));
typeConverter.ConvertFromString(null);
当 Microsoft文档指出默认实现始终返回null时.
When the Microsoft documentation remarks that the default implementation always returns null.
推荐答案
TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(string));
typeConverter.CanConvertFrom(null); //返回"false"的地方表示不可转换
typeConverter.ConvertFromString(null);
还需要字符串参数作为输入-> http://msdn.microsoft.com/zh-CN/library/25ds5cb6.aspx
并且string的默认值为String.Empty,它仍在挖掘问题,并将在此处提出更多的事实...
TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(string));
typeConverter.CanConvertFrom(null); //where it return "false" means not convertable
typeConverter.ConvertFromString(null);
also it required string parameter as input-> http://msdn.microsoft.com/en-us/library/25ds5cb6.aspx
and the default value of string is String.Empty, still digging the issue and will come up with more facts over here...
public Object ConvertFromString(
string text
)
Parameters
textType: System.String
The text representation of the object to convert.
Return Value
Type: System.Object
An Object that represents the converted text.
也请尝试以下代码块及其返回的预期结果:
also try this block of code and its returning desired outcome:
public Object ConvertFromString(string text)
{
return text;
}
ConvertFromString("test string"); //return "test string"
ConvertFromString(string.Empty); // return ""
ConvertFromString(null); // return null
请问我需要相同的任何内容.
Ask me if required any specific in same..
这篇关于ConvertFromString不返回默认null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文