简体   繁体   English

更新:从用户控件上的自定义控件覆盖方法

[英]Update: Override method from custom control on user control

good morning. 早上好。

this is something that I'd like to achieve. 这是我想要实现的目标。

for CustomControlA , I have 对于CustomControlA ,我有

public void OverrideableMethod()
{
// some codes here
}

and I have UserControlB . 我有UserControlB I would like to "override" the method from CustomControlA on UserControlB . 我想从“越权”的方法CustomControlAUserControlB is this possible? 这可能吗? if it is, how to? 如果是,该怎么办?

the best thing I can come up is to create a method of the same name on UserControlB and invoke CustomControlA.OverrideableMethod() from there. 我能想到的最好的办法是在UserControlB上创建一个同名的方法,然后从那里调用CustomControlA.OverrideableMethod()


Update: Thanks for the answers, I appreciate them. 更新: 感谢您的回答,我非常感谢。

the code is something like this: 代码是这样的:

public class CustomControlA : CustomControl
{
    public virtual void OverrideableMethod()
    {
        // do something
    }
}

public class UserContorlB : System.Web.UI.UserControl
{
    protected CustomControlA someVariableForCustomControlA;

    // here lies the problem, on how to override CustomControlA.OverrideableMethod() here
    // without inheriting CustomControlA itself
}

here lies to problem. 这就是问题所在。 as much as possible, I'd want to keep away from inheriting CustomControlA, for reasons allowing UserControlB to inherit one more base class. 我想尽可能地避免继承CustomControlA,原因是允许UserControlB继承另一个基类。 is this possible to override CustomControlA.OverrideableMethod() given the scenario above? 在上述情况下,是否有可能重写CustomControlA.OverrideableMethod()

You need to use virtual and override keywords to override the methods in the child class if at all UserControlB is a child class of CustomControlA. 如果UserControlB完全是CustomControlA的子类,则需要使用virtual和override关键字来覆盖子类中的方法。 If not then its not method overriding. 如果不是,则其不是方法重写。

public class CustomControlA
{
     public virtual void OverrideableMethod()
     {
      // some codes here
      }
}

public class UserControlB: CustomControlA
{
     public override void OverrideableMethod()
     {
         // some codes here
     }

}

UPDATE (based on the updated part of your question): No its not possible to do the overriding(actually you are not even trying to do overriding) but you can do something like below. 更新(根据问题的更新部分):不可能进行覆盖(实际上您甚至没有尝试进行覆盖),但是您可以执行以下操作。 You should always call UserContorlB.OverrideableMethod() but make sure you have some condition based on which the proper method will be called. 您应该始终调用UserContorlB.OverrideableMethod()但请确保您具有一些条件,基于此条件将调用正确的方法。

public class CustomControlA
{
    public void OverrideableMethod()
    {
        // do something
    }
}

public class UserContorlB : System.Web.UI.UserControl
{
    protected CustomControlA someVariableForCustomControlA;

    public void OverrideableMethod()
    {
        if(some condition is true)
        {
             someVariableForCustomControlA.OverrideableMethod();
        }
        else 
        {
             // do some logic here
        }   
    }
}

You should post your code for the controls to make this clear. 您应该为控件添加代码以使其清楚。

It is possible under the following conditions, and btw you wouldn't be invoking CustomControlA.OverrideableMethod() from UserControlB. 在以下情况下是可能的,并且顺便说一句,您不会从UserControlB调用CustomControlA.OverrideableMethod()。 If UserControlB inherited CustomControlA then the method exists in UserControlB: 如果UserControlB继承了CustomControlA,则该方法存在于UserControlB中:

public class CustomControlA : UserControl
{

    public virtual void OverrideableMethod() {  /* code the default implementation */ }
}

Your UserControlB would extend/inherit CustomControlA 您的UserControlB将扩展/继承CustomControlA

public class UserControlB : CustomControlA
{

    public override void OverrideableMethod() {  /* implement whatever you want */ }
}

If CustomControlA's method had code you wanted to execute as well as NEW code in UserControlB you would do this: 如果CustomControlA的方法具有您要执行的代码以及UserControlB中的NEW代码,则可以执行以下操作:

public class UserControlB : CustomControlA
{

    public override void OverrideableMethod() 
    {
         base.OverrideableMethod();
         /*
            additional code here
         */  
    }
}

The best practice for inherited methods when building base classes that will fire descendant code is as follows: 构建将触发后代代码的基类时,继承方法的最佳做法如下:

public class CustomControlA : UserControl
{

    public void OverrideableMethod() 
    {
       //You can code here for things before the overrideable method
       OnOverrideableMethod();
       //You can code here for things after the overrideable method
    }

    protected virtual void OnOverrideableMethod() { }
}


public class UserControlB : CustomControlA
{

    protected override void OnOverrideableMethod() 
    {
       /*  implement here */
    }
}

This prevents any direct calls to the "OnXXXX" methods and forces calls to the public/well defined methods. 这样可以防止直接调用“ OnXXXX”方法,并强制调用公共/定义良好的方法。 It provides you the ability to add code before and/or after the "OnXXX" method. 它使您能够在“ OnXXX”方法之前和/或之后添加代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM