Fra*_*rme 8 .net c# asp.net-mvc
我试图在动作过滤器中向下转换一个控制器实例,我有问题这样做.
我有一DefaultController节课:
public abstract partial class DefaultController<T> : Controller where T : IBaseEntity
{
}
Run Code Online (Sandbox Code Playgroud)
IBaseEntity 是:
public interface IBaseEntity
{
int Id { get; set; }
DateTime CreatedOn { get; set; }
DateTime ModifiedOn { get; set; }
int CreatedBy { get; set; }
int ModifiedBy { get; set; }
int OwnerId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个控制器的实例,继承DefaultController:
public class WorkflowController : DefaultController<Workflow>
{
}
Run Code Online (Sandbox Code Playgroud)
Workflow是继承BaseEntity哪个实现IBaseEntity.
现在,在我的动作过滤器中,代码明智,不可能知道请求在哪个控制器上运行,所以我试图将它向下转换为 DefaultController
public class AddHeaders : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var defaultControllerGenericType = controller.GetType().BaseType.GenericTypeArguments.FirstOrDefault();
//this retrieve with which type DefaultController was initiated with...
var controller = filterContext.Controller as DefaultController<BaseEntity>;
//this returns null...
var controller2 = filterContext.Controller as DefaultController<IBaseEntity>;
//this does so as well.
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试过使用defaultControllerGenericType但我无法将其传递到任何地方,或者至少我缺乏正确的语法.
有没有办法做到这一点?
理解为什么这是不可能的,这是有益的.
public abstract partial class DefaultController<T> : Controller where T : IBaseEntity { ... }
public class WorkflowController : DefaultController<Workflow> { ... }
Run Code Online (Sandbox Code Playgroud)
在这里,让我改写一下:
class Container { }
class Animal { }
class Fish : Animal { }
class Cage<T> : Container where T : Animal
{
public T Contents { get; set; }
}
class Aquarium : Cage<Fish> { }
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当我有以下情况时会发生什么:
Aquarium a = new Aquarium(); // Fine
Cage<Fish> cf = a; // Fine
Container c = a; // Fine
Cage<Animal> ca1 = a; // Nope
Cage<Animal> ca2 = a as Cage<Animal>; // Null!
Run Code Online (Sandbox Code Playgroud)
为什么不允许这样做?好吧,假设它被允许.
Cage<Animal> ca = a; // Suppose this is legal
Tiger t = new Tiger(); // Another kind of animal.
ca.Contents = t; // Cage<Animal>.Contents is of type Animal, so this is legal
Run Code Online (Sandbox Code Playgroud)
我们只是将一只老虎放入水族馆.这就是为什么这是非法的.
正如Jon Skeet所说,一碗苹果不是一碗水果.你可以把香蕉放入一碗水果中,但你不能把香蕉放入一碗苹果中,因此它们是不同的类型!