Overriding a protected internal virtual method in C #

I'm trying to build such a class ...

public class IoCControllerFactory : DefaultControllerFactory { protected override IController GetControllerInstance(RequestContext request_context, Type controller_type) { // Attempt to resolve controller type. IController resolvedController = null; if (controller_type != null) { if (!typeof(IController).IsAssignableFrom(controller_type)) { throw new ArgumentException(string.Format("Type requested is not a controller: {0}", controller_type.Name), "controller_type"); } resolvedController = _container.Resolve(controller_type) as IController; } // Throw page not found if controller does not exist. if (resolvedController == null) { throw new HttpException((int)HttpStatusCode.NotFound, "The requested page was not found."); } return resolvedController; } } 

The method tries to override the internal virtual method with the subsequent signature (from the .net assembly ... I cannot change this)

 protected internal virtual IController GetControllerInstance( RequestContext requestContext, Type controllerType ) 

When I try to compile, I get the following ...

 'XXX.Web.Mvc.IoCControllerFactory.GetControllerInstance(System.Web.Routing.RequestContext, System.Type)': no suitable method found to override 

It seems like everyone on the Internet can do it just fine, is there something obvious that I'm missing?

+4
source share
2 answers

I just ran into this problem; If the selected solution was not your problem, then it is that the RequestContext forwared type from the System.Web.Routing assembly in System.Web is one. The solution is to reference System.Web and System.Web.Routing.

+1
source

New (possible) answer

I can duplicate this problem if I define a type with the same name as one from the "System.Web.Mvc.dll" assembly in my own library. Could this be your problem? See the code below:

 using System; using System.Web.Mvc; using System.Web.Routing; namespace SystemWebMvcTest { // See here: I've declared a type with the same name as a type // from the System.Web.Mvc namespace in System.Web.Mvc.dll. public interface IController { } public class IoCControllerFactory : DefaultControllerFactory { // Now this method signature, since it does not include the fullly // qualified name of its return type, is actually defined to return // an instance of the IController interface defined in THIS assembly, // rather than the System.Web.Mcv.IController interface. protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { return null; } } } 

The same error occurs if you have a name conflict with any of the corresponding types: DefaultControllerFactory , IController , RequestContext or Type .


Old answer

The original method is protected internal , which means that it can be accessed by any type inside the same assembly, as well as any derived type; but you are trying to override it with the protected method, which removes access to all non-derived types, even in the same assembly as the base type.

This is not so different from trying to override the public member with private . You cannot do this; you will need to declare the derived member new instead of override (this is a pretty bad idea, almost all the time).

Mark your IoCControllerFactory.GetControllerInstance protected internal method and you should be good.

UPDATE . In fact, as Thomas pointed out in a comment, it completely depends on whether your derived type is in the same assembly as the base type. If not, declaring a protected internal derived member does not make sense. If you have a different assembly, then I'm not sure why defending a protected member causes an error, as that is exactly what you should be doing.

UPDATE 2 : It appears that in the code you posted, your base and derived types are in the same assembly. This is why the IoCControllerFactory.GetControllerInstance method must be protected internal in order to match the availability of its base type. If you were in a separate assembly, internal would be wrong, as it would open accessibility to an entire new set of types (in this new assembly). If you saw code examples on the Internet where the type in one assembly inherited the protected internal member from the base type in another, this explains why the derived member was only protected - the same for accessibility.

UPDATE 3 : Nothing! Apparently, the base type is in the .NET assembly, which is clearly different from any assembly in which your derived type is located. Based on this, your method signature should actually compile as is, as far as I can tell. My one question is whether all these names are present - DefaultControllerFactory , IController , RequestContext , etc. - in your assembly. If you have a name conflict, this may be one possible explanation. Otherwise, I do not understand the compiler error you are getting.

+2
source

All Articles