Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何正确隐藏基本方法_C#_Oop - Fatal编程技术网

C# 如何正确隐藏基本方法

C# 如何正确隐藏基本方法,c#,oop,C#,Oop,我所拥有的 public abstract class CustomWebViewPage<T> : WebViewPage<T> { public new CustomUrlHelper Url { get; set; } public UrlHelper UUrl { get; set; } public override void InitHelpers() { Url = new CustomUrlHelper(

我所拥有的

public abstract class CustomWebViewPage<T> : WebViewPage<T>
{
    public new CustomUrlHelper Url { get; set; }

    public UrlHelper UUrl { get; set; }

    public override void InitHelpers()
    {
        Url = new CustomUrlHelper(ViewContext.RequestContext);           

        UUrl = new UrlHelper(ViewContext.RequestContext);

    }
}

public class CustomUrlHelper : UrlHelper
{
    public CustomUrlHelper(RequestContext requestContext) : base(requestContext)
    {
    }

    public CustomUrlHelper(RequestContext requestContext, RouteCollection routeCollection) : base(requestContext, routeCollection)
    {
    }

    public string Action(string actionName)
    {
        return this.GenerateUrl(null, actionName, null, null);
    }
}
但是:

如何组织投射或隐藏,因为这种构造没有意义

 public new UrlHelper Url { get; set; }

 Url = new CustomUrlHelper(ViewContext.RequestContext) as UrlHelper;
只调用子构造函数,但不调用方法

UPD:基本方法操作不是虚拟的

我认为您要查找的只是Url=new CustomUrlHelperViewContext.RequestContext

如果每个CustomViewWebPage只需要一个UrlHelper实例,那么就太复杂了。继承的好处在于,无论出于何种目的,孩子都是父母。在您的例子中,CustomUrlHelper是一个UrlHelper,所以您不需要两者都需要。使用以下命令:

    public abstract class CustomWebViewPage<T> : WebViewPage<T>
    {
        public UrlHelper Url { get; set; }

        public override void InitHelpers()
        {
            if (true){Url = new CustomUrlHelper(ViewContext.RequestContext);}
            else{Url = new UrlHelper(ViewContext.RequestContext);}

        }
    }
您不需要将CustomUrlHelper强制转换为UrlHelper,因为它已经是通过继承实现的

您还应该将新操作方法的签名更改为public override string Actionstring actionName


编辑:这适用于.NET 4.5。如果要隐藏而不是覆盖.NET 4,请关闭覆盖修改器。您仍将以相同的方式调用方法,如果您的实现是CustomUrlHelper,那么它将调用您的自定义版本,而不是基本方法

但基类中的“Action”方法不是虚拟的-您的示例不起作用。您使用的是什么UrlHelper?在System.Web.Mvc中确实如此:但在元数据中,它看起来像这个公共字符串Actionstring actionName,object RouteValue;啊,对不起。。。看起来它在.NET4.5中是虚拟的,但在4中不是,这可能就是为什么您认为它不是虚拟的。
    public abstract class CustomWebViewPage<T> : WebViewPage<T>
    {
        public UrlHelper Url { get; set; }

        public override void InitHelpers()
        {
            if (true){Url = new CustomUrlHelper(ViewContext.RequestContext);}
            else{Url = new UrlHelper(ViewContext.RequestContext);}

        }
    }