为什么var returnText =值作为字符串不起作用?

why var returnText = value as string not working?
2021-06-10
  •  译文(汉语)
  •  原文(英语)

我有一个实现IValueConverter的Convert方法.我的第一个语句是var returnText = value as string不起作用,无法接管value作为string.但是,它不起作用,在调试时,我发现"值"变量的值未分配给returnText,因此returnText始终为null.真奇怪 有人知道为什么吗?

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var returnText = value as string;

        if (!string.IsNullOrEmpty(returnText))
        {
           .....
速聊1:
您确定值的类型是字符串吗?
速聊2:
您确定value类型string吗?如果不是,value as string将返回.nullvaluestring
解决过程1

问题与解决方案

问题是as string不进行转换,只是将对象转换为字符串.如果那不起作用,它将产生null.该对象最初必须是字符串类型,在这种情况下显然不是这种情况.

在您的情况下可能可行的一种选择是调用.ToString()对象,如下所示:

var returnText = value.ToString();

但是请注意,这确实取决于您的对象类型及其.ToString()实际产生的结果.您可能会得到一个意想不到的价值.


其他建议

正如Tim所言,ToString()如果对象null始于,将抛出异常.建议在调用对象的任何方法之前进行测试.这样的事情会做:

string returnText = null;
if(value != null)
    returnText = value.ToString();

进一步阅读

有关更多信息,请参见此处.该链接提供了两个有用的引号:

as运算符就像强制转换操作.但是,如果无法进行转换,则as返回null而不是引发异常.

请注意,as运算符仅执行参考转换,可为空的转换和装箱转换.as运算符不能执行其他转换,例如用户定义的转换,而应使用强制转换表达式来执行.

速聊1:
请注意,如果valuenull,则将引发异常.(value ?? "").ToString()如果您想将其默认为空字符串,则可以这样做.
速聊2:
.: 好点子.请注意,我没有使用您的一线代码,因为我想使代码尽可能不言自明.如果他们不熟悉语法,那么您的示例可能会使OP感到困惑
解决过程2

尝试

var returnText = value==null ? "": value.ToString();
解决过程3

因为此var returnText = value作为字符串;表示:尝试值强制转换为string否,返回null.

作为(C#参考)http://msdn.microsoft.com/zh-cn/library/vstudio/cscsdfbt.aspx

为什么用System.Convert.ToString()而不是.ToString()......

我宁愿使用,var returnText = System.Convert.ToString(value);因为这将使用该IConvertable接口.ToString()可能会产生错误的结果.(例如类名等)

详细信息:Convert.ToString方法 http://msdn.microsoft.com/zh-cn/library/astxcyeh.aspx

速聊1:
看起来Convert不起作用,因为它本身就是Convert方法.
速聊2:
您应该使用全名(带有名称空间):System.Convert.ToString()

I have a Convert method that implements IValueConverter. My first statement is var returnText = value as string not working to take over the value as string. However, it doesn't work and when I was debugging, I found out the value of the "value" variable didn't assign over to returnText so the returnText is always null. It is odd. Does anybody know why?

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var returnText = value as string;

        if (!string.IsNullOrEmpty(returnText))
        {
           .....
Talk1:
are you sure that the type of value is string?
Talk2:
Are you sure value is of the type string? value as string will return null if value is not a string.
Solutions1

Problem and Solution

The problem is that as string does not convert, it just casts the object to a string. If that doesn't work it will produce null instead. The object must be of string type originally, in this case that is obviously not the case.

One option that would likely work in your case is to call .ToString() on the object like so:

var returnText = value.ToString();

but please note that this really does depend on your object type, and what it's .ToString() method actually produces. You may get a value that you do not expect.


Additional Recommendations

As Tim has commented, ToString() will throw an exception if the object is null to begin with. It is recommended to test for this before calling any method on the object. Something like this will do:

string returnText = null;
if(value != null)
    returnText = value.ToString();

Further Reading

See here for more information. A couple of useful quotes from that link:

The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.

and

Note that the as operator performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.

Talk1:
Note that if value is null, this will throw an exception. You could do (value ?? "").ToString() if you want to default it to an empty string.
Talk2:
.: Good point. Note that I didn't use your one-liner as I wanted to make the code as self-explanatory as possible. Your example would likely confuse the OP if they are not familiar with the syntax
Solutions2

try

var returnText = value==null ? "": value.ToString();
Solutions3

Because this var returnText = value as string; means: Try to cast value as string if not, return null.

as (C# Reference) http://msdn.microsoft.com/en-us/library/vstudio/cscsdfbt.aspx

Why System.Convert.ToString() instead of .ToString()......

I'd rather use var returnText = System.Convert.ToString(value); because this will use the IConvertable interface. The ToString() could give a screwed-up result. (like classnames etc)

More info: Convert.ToString Method http://msdn.microsoft.com/en-us/library/astxcyeh.aspx

Talk1:
It looks Convert won't work because it is a Convert method itself.
Talk2:
You should use the fullname (with namespace): System.Convert.ToString()
转载于:https://stackoverflow.com/questions/19095309/why-var-returntext-value-as-string-not-working

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入