I have four classes. Request, DerivedRequest, Handler, DerivedHandler. The Handler class has a property with the following declaration:
public abstract Request request { get; set; }
The DerivedHandler needs to override this property so that it returns DerivedRequest instead:
pub开发者_StackOverflow中文版lic override DerivedRequest request { get; set; }
Does anyone have any ideas about how to make this work?
This isn't really a good way to structure things. Do one of the following
1) Just don't change the return type, and override it normally in the subclass. In DerivedHandler
you can return an instance of DerivedRequest
using the base class signature of Request
. Any client code using this can choose to cast it to DerivedRequest
if they want to.
2) Use generics instead if they are not supposed to be polymorphic.
public abstract class HandlerBase<T> where T: Request
{
public abstract T Request {get;set;}
}
public class Handler: HandlerBase<Request>()
public class DerivedHandler: HandlerBase<DerivedRequest>()
In the C# language you are not allowed to change the signature of an inherited method, unless you substitute it with another method with the same name. This technique is referred to as "member hiding" or "shadowing".
If you are using .NET 2.0 or later, you could solve this problem by turning the return type of the Request
property into a generic type parameter of the Handler
class. The DerivedHandler
class would then specify the DerivedRequest
class as argument for that type parameter.
Here's an example:
// Handler.cs
public class Handler<TRequest> where TRequest : Request
{
public TRequest Request { get; set; }
}
// DerivedHandler.cs
public class DerivedHandler : Handler<DerivedRequest>
{
}
Except for hiding the original property:
public new DerivedRequest Request { get;set;}
However, I strongly advise against that. Hiding something supposed to be overriden is inviting trouble, especially if the property isn't a simple auto generated one. Also, if using it as an interface or base class, the original implementation (in that case, one class higher in the inheritance tree). If you are implementing an abstract class or interface, you won't even be able to hide the original signature, as you are required to implement it.
Usually, if you think about using the new
keyword, you are on the wrong track. There are cases where it is necessary and required, however, in most cases, it isn't.
Instead, make another property:
public DerivedRequest DerivedRequest {/* make adequate conversions here*/ }
That way, you are on the clear side concerning OOP and you get your information in a clear way.
Edit:
You can't change the type on a derived type, but new
might help:
In the derived type...
public new DerivedRequest request
{
get{return (DerivedRequest) base.request;}
set{base.request = value;}
}
public override Request request
{
get{return base.request;}
set{base.request = (DerivedRequest) value;} // Throws InvalidCastException if misused.
}
This is not theoretically possible. The override must be covariant for return type (that is, the return type must be more specific or the same), and contravariant for parameter (that is, parameter type must be less specific or the same). So your new type must be effectively at the same time covariant and contravariant with respect to the Request
-- that means, the only possible type is just Request
.
For this reason, it's not allowed in C# to change the type of properties for overrides.
public class Request{}
public class DerivedRequest : Request{}
public class Handler<T>
where T : Request
{
public abstract T Request { get; set; }
}
public class DerivedHandler : Handler<DerivedRequest>
{
public override DerivedRequest Request { get; set; }
}
精彩评论