使用三元运算符了解 C# 编译错误 [英] Understanding C# compilation error with ternary operator
问题描述
我已从 Wrox Professional ASP.NET 4.0 MVC 4
一书第 179 页(了解 Web 应用程序中的安全向量"一章)中复制了以下代码,并对其进行了少量修改 protected
并在我抽象的应用程序范围 Controller
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 方法"?
由于 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 类
Suppose I have class A, B and 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屋!