C# C使用另一个THIS调用Base方法

C# C使用另一个THIS调用Base方法,c#,C#,我在基类中有以下方法: public class Base { protected string Make(string param) { return this.ClientID + "_" + configParam; } } 我还有一节课 public class Class2 : Base { } 及 这可能吗 谢谢。假设您只希望Base的子类调用Make,即使它不是自己的Make,您也可以添加一个受保护的调用器方法,

我在基类中有以下方法:

public class Base  
{
protected string Make(string param)
        {
            return this.ClientID + "_" + configParam;
        }
}
我还有一节课

public class Class2 : Base  
{

}

这可能吗


谢谢。

假设您只希望Base的子类调用Make,即使它不是自己的Make,您也可以添加一个受保护的调用器方法,我称之为InvokeSiblingMake:


在您拥有的类的结构中,Class3是Class2的兄弟。你是从父母那里继承的,不是你的兄弟姐妹。这意味着无法通过base从Class3内部干净地调用Class2上的方法


通过给Class3一个Class2成员变量,有很多方法可以解决这个问题,但是此时您并没有使用继承。在这种方法中,类2可以从System.Object、System.DateTime或您自己创建的任何其他类继承。然后,您将对象集作为属性进行操作,而不是通过继承进行操作。

简短回答。没有人问尼科西这个问题。根据你所拥有的类的结构,类3是类2的兄弟。这些类彼此无关。从生物学角度考虑,你从父母那里继承了你的基因,而不是你的兄弟姐妹。将Class2 InstanceJ注入Class3,然后打电话给Class2 Instance.make。这是可行的,您可以在Class3中实例化Class2的一个实例,并对其调用方法。“但是你真的需要重新思考你的结构,这是没有意义的。”克里加从生物学角度思考,这就像跑到你的父母那里,让他们让你的兄弟洗碗:P
public class Class3 : Base  
{
 //HERE i would like to call Make but with the THIS as Class2, not the current - Class3.
}
public class Base
{
    private string ClientID;
    protected string Make(string param)
    {
        return this.ClientID + "_" + param;
    }

    protected void InvokeSiblingMake(Base other)
    {
        other.Make("hello world");
    }
}

public class Class2 : Base  
{

}

public class Class3 : Base
{
    //HERE i would like to call Make but with the THIS as Class2, not the current - Class3.
    public void Test(Class2 other)
    {
        InvokeSiblingMake(other);
    }
}