How to Get Base Class Instance from a Derived Clas

2019-04-04 18:03发布

I don't know if this is possible, but I am trying to get the Base Class instance from a Derived Class. In C#, I can use the base keyword to access properties and methods of the Base Class (of course), but I want to use base itself. Attempting to do so results in a "Use of keyword 'base' is not valid in this context" error.

Example Code

public class SuperParent
{
    public int SPID;

    public SuperParent()
    {
    }
}

public class SubChild : SuperParent
{
    public SubChild(int pSPID)
    {
        base.SPID = pSPID;
    }

    public int BaseSPID
    {
        get
        {
            SuperParent sp = base;
            return sp.SPID;
        }
    }
}

7条回答
干净又极端
2楼-- · 2019-04-04 18:22

Point 1: if you want to create the base class instance within child class than it does not worth. You already have public things accessible in child.

Point 2: If you have initialized child class and now want to get base class "instance" then how can you get that if it's not initialized(Because now the base class instance is not present in the physical memory, and there is just child class instance there)?

查看更多
对你真心纯属浪费
3楼-- · 2019-04-04 18:23

The derived instance IS the base instance. It's just one object instance in memory.

example:

public class A : B 
{
}

var thing = new A();

thing is an instance of an A, and is also an instance of a B.
You could for example, write this line:
B thing2 = thing;

查看更多
地球回转人心会变
4楼-- · 2019-04-04 18:24
 class Parent
 {
      private Parent _parent;
      public Parent()
      {
         _parent = this;
      }

     protected Parent GetParent()
     {
         return _parent;
     }
 }

class Child : Parent
{
    private Parent _parent;
    public Child()
    {
        _parent = base.GetParent();
    }
}
查看更多
何必那么认真
5楼-- · 2019-04-04 18:25

the problem hasn't been explained as clearly as it could. however, typically, you may be better to use an abstract base class and methods and then override the required methods. you can then use the base.method as required in this case (otherwise you'll have just spun up an instance of the derived class).

public abstract class foo {

   public virtual void bar(){..}
}

public class footwo : foo {
   public override void bar(){
       // do somethng else OR:
       return base.bar();
   }
}

}

查看更多
做个烂人
6楼-- · 2019-04-04 18:27

If you're working with an instance of the derived class, there is no base instance. An example:

class A
{
    public void Foo() { ... }
}

class B : A
{
    public void Bar() { ... }
}

What is not possible within B:

public void Bar()
{
    // Use of keyword base not valid in this context
    var baseOfThis = base; 
}

You can do something like this:

public void Bar()
{
    base.Foo();
}

And you can add another method like

public A GetBase()
{
    return (A)this;
}

And then you can

public void Bar()
{ 
    var baseOfThis = GetBase();
    // equal to:
    baseOfThis = (A)this;
}

So this GetBase() method is probably what you want.

The punchline is: If you have an instance of B, it inherits all properties and the non-overriden behaviour of A, but it does not consist of an instance of B which holds an (hidden but automatic) reference to an instance of A. You can cast your B instance to A, but it remains to be an instance of B.

查看更多
\"骚年 ilove
7楼-- · 2019-04-04 18:35

Well you not provide code for your question, but i supsect you want something like

class Base
{
    public virtual void Foo()
    {
        Console.WriteLine("base");
    }
}

class Derived : Base
{
    public override void Foo()
    {
        Console.WriteLine("derived");
    }

    //// bad
    //public Base MyBase
    //{
    //    get
    //    {
    //        return base; // Use of keyword 'base' is not valid in this context
    //    }
    //}

    // work but...
    public Base MyBase
    {
        get
        {
            return (Base)this;
        }
    }
}

But keep in mind that MyBase is really of type Derived

new Derived().MyBase.Foo(); // output "derived"
查看更多
登录 后发表回答