[Mono-list] An inheritance dilemma

Francisco M. Marzoa fmmarzoa at gmx.net
Sun Nov 21 13:13:17 EST 2010


Hello,

I'll start by the code, because I think it's the best way of
illustrating the problem in this case:

using System;

namespace Dummy
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            B b = new B();
            b.SampleMethod();
        }
    }
   
    public class A {
        protected virtual string GetClassName() {
            return "Class A";
        }
       
        protected string BuildString () {
            return this.GetClassName() + " method";
        }
       
        public virtual void SampleMethod () {
            Console.WriteLine (this.BuildString());
        }
    }
   
    public class B : A {
        protected override string GetClassName() {
            return "Class B";
        }
       
        public override void SampleMethod () {
            base.SampleMethod();
            Console.WriteLine (this.BuildString());
        }
    }
   
}


The output of this code is:

Class B method
Class B method

While what I want to get is:

Class A method
Class B method

I've tried with some minor changes, like removing "virtual" and
"override" from GetClassName, getting:

Class A method
Class A method

But I didn't been able to get the output that I want. Perhaps using
reflection???




More information about the Mono-list mailing list