用三元运算符理解C#编译错误 [英] Understanding C# compilation error with ternary operator

查看:102
本文介绍了用三元运算符理解C#编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从 Wrox Professional ASP.NET 4.0 MVC 4 一书,第179页(了解Web应用程序中的安全性向量一章)中复制了以下代码使其成为 protected 并在我的抽象应用程序范围内以实用工具方式存储控制器

I have copied the following code from Wrox Professional ASP.NET 4.0 MVC 4 book, page 179 (Chapter "Understanding the Security Vectors in a Web Application") with the little modification of making it protected and storing as utility method in my abstract application-wide Controller

protected ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }
}

上面的代码旨在保护MVC应用程序免受打开重定向攻击,这不是问题的主题。

The code above is aimed at securing the MVC application from open redirection attacks, which are not subject of the question.

代码显然格式正确,编译并且我相信它有效。

The code is obviously well-formed, compiles and I trust it works.

当巧妙地将上面的代码更改为以下单行时出现问题

The problem arises when "smartly" changing the code above into the following one-liner

return (Url.IsLocalUrl(returnUrl)) ? Redirect(returnUrl) : RedirectToAction("Index", "Home");

上面的单行应该与扩展代码完全相同(不,ReSharper没有建议我替换,这是我的主动。)

The one-liner above is supposed to do exactly the same as the extended code (no, ReSharper didn't suggest me to replace, it was my initiative).

编译错误如下: System.Web之间没有隐式转换.Mvc.RedirectResult和System.Web.Mvc.RedirectToRouteResult

然后ReSharper来帮助并建议以下修改

ReSharper then comes to help and suggests the following modification

return (Url.IsLocalUrl(returnUrl)) ? (ActionResult) Redirect(returnUrl) : RedirectToAction("Index", "Home");



问题是我为什么要投出重定向方法?



由于 Redirect RedirectToAction 都返回 ActionResult的子类(通过F12验证)并且该子类是函数的返回值,它应该是自动兼容的。或者至少,根据我对C#的了解,要么两个代码都编译,要么两者都不编译。

The question is "why do I have to cast Redirect method"?

Since both Redirect and RedirectToAction return a subclass of ActionResult (verified via F12) and that subclass is the return value of the function, it should be automatically compatible. Or at least, in my knowledge of C#, either both codes compile or they both don't.

更一般地,问题可以重新表述如下:

To be more general, the question could be reformulated as follows:

假设我有A,B和C类

abstract class A {}
class B: A{}
class C: A{}

并假设以下函数有效

private A Function(){
    if (condition) return new B();
    else return new C();
}

为什么下面的单线程不能编译?

Why the following one-liner doesn't compile?

private A Function(){
    return (condition) ? new B(): new C();
}


推荐答案

它不能编译因为编译器根据两个返回值之一决定一行的返回类型。由于类型B不是从类型C派生,或者反过来,这两种类型不是互斥的,并且if(作为一个整体)的返回类型不能以这种方式派生。

It doesn't compile because the compiler decides what the return-type of the one-liner is based upon one of the two return-values. Since type B doesn't derive of type C, or the other way around, the two types are not mutually exclusive, and the return type of the if (as a whole) cannot be derived that way.

如果您想在单行中执行此操作,则必须将返回值强制转换为要返回的类型,即类型A.

If you would want to do it in a one-liner, you have to cast the return-values to the type that you want to return, which is type A.

private A Function(){
    return (condition) ? ((A)new B()): ((A)new C());
}

更新:我没有看到你已经在你的问题中进行了投射,我的道歉。

UPDATE: I didn't see that you already do the casting in your question, my apologies.

这篇关于用三元运算符理解C#编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆