Overriding an abstract property with a derived return type in c#

前端 未结 6 1703
独厮守ぢ
独厮守ぢ 2021-02-06 23:20

I have four classes. Request, DerivedRequest, Handler, DerivedHandler. The Handler class has a property with the following declaration:

public abstract Request         


        
6条回答
  •  心在旅途
    2021-02-07 00:20

    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.
    }
    

提交回复
热议问题