Call a method in every derived class

时间:2016-10-20 13:23:36

标签: c# inheritance

Is it a way to call a method in each derived class, hierarchically?

I need it to use it with user scripts, so i cant force the user to call base.Update();

I need it to be called whether he did the base call or not.

    public class Base
    {
        public virtual void Update()
        {
            Console.WriteLine("BASE UPDATE");
            ;
        }
    }
    public class Base2 : Base
    {
        public override void Update()
        {
            Console.WriteLine("BASE2 UPDATE");
            ;
        }
    }
    public class Base3 : Base2
    {
        public override void Update()
        {
            // base.Update() ? no no, i want to do it for User Scripts so i cant force user to call base.Update()
            Console.WriteLine("BASE3 UPDATE");
        }
    }
    public class TestClass
    {
        public void DoTheTest()
        {
            Base3 instance = new Base3();
            instance.Update();
            // the output i need:
            /*
                BASE UPDATE
                BASE2 UPDATE
                BASE3 UPDATE
            */

        }
    }

2 个答案:

答案 0 :(得分:0)

不是一种优雅的方式,但它有效

public interface IUpdateHandler
{
    void UpdateHandler();
}

public class Base : IUpdateHandler
{
    public Base()
    {
        OnUpdate += (s, e) => UpdateHandler();
    }

    public void Update()
    {
        OnUpdate(this, null);
    }

    public void UpdateHandler()
    {
        Console.WriteLine("BASE UPDATE");
    }

    protected event EventHandler OnUpdate;
}

public class Base2 : Base, IUpdateHandler
{
    public Base2()
    {
        OnUpdate += (sender, args) => UpdateHandler();
    }

    public new void UpdateHandler()
    {
        Console.WriteLine("BASE2 UPDATE");
    }
}

public class Base3 : Base2, IUpdateHandler
{
    public Base3()
    {
        OnUpdate += (sender, args) => UpdateHandler();
    }

    public new void UpdateHandler()
    {
        Console.WriteLine("BASE3 UPDATE");
    }
}

答案 1 :(得分:0)

所以你有一个Base类的对象,或Base和的派生类的对象 你想以层次结构的顺序从上到下调用​​所有可用的Update()函数(base1 / base2 / base3)

您可以从任何对象获取Type对象。您也可以要求基类类型。从每个Type对象,您可以询问它是否知道具有特定名称的方法。如果是,您可以调用此方法。

  • Object.GetType()
  • Type.BaseType
  • Type.GetMethod(字符串)
  • MethodInfo.Invoke

public void UpdateHierarchical(Base someObject)
{
    UpdateHierarchical(someObject.GetType(), someObject);
}

private void UpdateHierarchical(Type type, object obj)
    // if the object has a base class
    // call this function recursively with type = base type
    // if this type has has an Update, call it,

    Type type = obj.GetType();        // the type of obj
    Type baseType = type.BaseType();  // the base type of obj
    if (baseType != null)             // there is a base type
        UpdateHierarchical(baseType, obj);

    // now that all base Update() are called,
    // check if this type has an Update
    MethodInfo updateInfo = type.GetMethod("Update")
    if (updateInfo != null)
    {   // this type has an Update()
        updateInfo.Invoice(obj, null);
    }
}