簡體   English   中英

在每個派生類中調用一個方法

[英]Call a method in every derived class

它是一種在每個派生類中調用方法的方法嗎?

我需要它與用戶腳本一起使用,所以我不能強迫用戶調用base.Update();

無論他是否進行了基本呼叫,我都需要它。

    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
            */

        }
    }

不是優雅的方式,但它的工作原理

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

所以你有一個Base類的對象,或Base的派生類的對象,你想從上到下(base1 / base2 / base3)以層次結構的順序調用所有可用的Update()函數

從任何對象,您都可以獲得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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM