简体   繁体   English

访问基类中的派生类成员

[英]Access to a derived class member in a base classe

i want to access to a derived class member in the base classe: 我想访问基类中的派生类成员:

class Program
{
    static void Main(string[] args)
    {
        B b = new B();
        b.FieldTest = 5;
        b.MethodeTest();

    }
}

public class A
{
    public void MethodeTest()
    {
        //will return B
        Type t = this.GetType();
        Console.WriteLine(t);

        var temp = ???.FieldTest;
        //i want that these return 5
        Console.WriteLine(temp);
        Console.ReadLine();
    }
}

public class B:A
{
    public int FieldTest;
}

I'm not sure that these is possible but I wish that you have any idea to solve it. 我不确定这是否可行,但希望您有解决的办法。

Thank you 谢谢

Well you could do it with dynamic typing: 好吧,您可以使用动态类型做到这一点:

dynamic dynamicThis = this;
var temp = dynamicThis.FieldTest;

... but it's a very strange requirement. ...但这是一个非常奇怪的要求。 What would you expect to happen if this was actually just an instance of A , or indeed an instance of a different subclass without such a member? 如果this实际上只是 A一个实例,或者实际上是没有这样的成员的另一个子类的实例,您会发生什么? Basically it's a dodgy design. 基本上,这是一个狡猾的设计。

It's not clear what you're trying to achieve, but you might want to make A an abstract class with an abstract property which all subclasses could implement. 目前尚不清楚您要实现的目标,但您可能希望使A为具有所有子类都可以实现的abstract属性的抽象类。 (Note that you can't make a field abstract...) (请注意,您不能将字段抽象化...)

If this doesn't help, please give more details about why you're trying to do this in the first place. 如果这样做没有帮助,请首先提供有关为什么要尝试执行此操作的更多详细信息。

You cannot do that in the way you're showing it by your example. 您无法以示例所示的方式进行操作。 The base class does not know anything about the implementation of the derived class. 基类对派生类的实现一无所知。

However, what you can do, is define a virtual method or property in the base class, which can be implemented in the derived class, and returns the desired value (Template method pattern): 但是,您可以做的是在基类中定义一个虚拟方法或属性,该方法或属性可以在派生类中实现,并返回所需的值(模板方法模式):

public class A
{
   protected virtual int FieldTest { get { return 0; } }

   public void TestMethod()
   {
        Console.WriteLine ("FieldTest: " + FieldTest);
   }

}

public class B : A
{
   protected override int FieldTest { get { return 5; } }
}

public class C : A 
{
   protected override int FieldTest { get { return 10; } }
}

The base class does not have access to the fields of the derived one. 基类无权访问派生类的字段。 Being in need of doing this is probably an application structure mistake. 需要这样做可能是应用程序结构错误。

Make your method virtual, and override it in derived class. 使您的方法虚拟,并在派生类中重写它。 You can call base class implementation of method, but also you can add new behavior specific to derived class: 您可以调用方法的基类实现,也可以添加特定于派生类的新行为:

public class A
{
    public virtual void MethodeTest()
    {
        //will return B
        Type t = this.GetType();
        Console.WriteLine(t);
        Console.ReadLine();
    }
}

public class B:A
{
    public int FieldTest;

    public override void MethodeTest()
    {
        base.MethodeTest(); // base class implementation

        Console.WriteLine(FieldTest); // FieldTest is available here
        Console.ReadLine();
    }
}

Unless every A has a FieldTest property, you can't do this without checking whether the A is actually a B . 除非每个A都具有FieldTest属性,否则您必须先检查A是否实际上是B才能执行此操作。

var b = a as B;
if (b != null)
{
   b.FieldTest;
}

You should not have to access a derived class member from a base class. 您不必从基类访问派生类成员。 Instead, your design should allow the more derived type to provide an implementation and in turn make a call to a base class. 相反,您的设计应允许更多派生的类型提供实现,然后依次调用基类。 Or, if the call is shared across multiple derived types, then the logic should belong to the base. 或者,如果调用在多个派生类型之间共享,则逻辑应属于基础。

Because it appears that you need to perform the call with a single method, a normal override should work. 因为似乎您需要使用单个方法执行调用,所以正常的覆盖应该起作用。 A contrived Example, 人为的例子

class Animal
{
   public virtual bool Speak() { //return true.  Assume all animals make a sound }
}

class Dog : Animal
{
  public override bool Speak() { //Bark and return base.Speak() }
}

Now if Dog defines Eat() , Animal should not look to perform derived.Eat . 现在,如果Dog定义Eat() ,则Animal应该不会执行derived.Eat Instead, Eat should be defined in Animal and called by derived classes. 相反,Eat应该在Animal中定义,并由派生类调用。

Thank you for your response! 感谢您的答复! I tried this solution and it work very well 我尝试了这个解决方案,它工作得很好

public void MethodeTest()
    {
        //will return B
        Type t = this.GetType();
        Console.WriteLine(t);

        foreach (FieldInfo fieldInfo in t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
        {
            Console.WriteLine("{0}\t{1}:{2}", fieldInfo.FieldType.Name, fieldInfo.Name, fieldInfo.GetValue(this));

        }

        Console.ReadLine();
    }

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

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