简体   繁体   中英

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? 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. (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 .

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 . Instead, Eat should be defined in Animal and called by derived classes.

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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