Hook for working without delegates (Reflection)

Can reflection be used to bind one function to another without the use of delegates?

class A
{
    void Foo()
    {
    }
}

class B
{
    void Main()
    {
        A a = new A();
        a.GetType().GetMethod("Foo").AddHook(a, Func); //I want something like this
        a.Foo();
        //Func gets called
    }

    void Func()
    {
    }
}

Is there a way to call Funcafter a call Foowithout using events, delegates, or just calling Funcfrom inside Foo?

I need this so that my game user interface can update.

As I can see, most people come across this by adding a bunch of events to and subscribing B to them. Like this

class A
{public delegate void UICallback();public event UICallback onFoo;
    void Foo()
    {
    ‎    onFoo.Invoke();
    }
}

class B
{
    void Main()
    {
        A a = new A();
        ‎a.onFoo += Func;
        a.Foo();
    }

    void Func()
    {
    }
}

, , , , (, 5 10), , , UI (, invoke onBattleStarted StartBattle()). , , , .

, , ... , Func Foo - Foo, .. Foo, , . , Foo, Func

!

+4
2

Func().

Class A
{
   void Foo()
   {
   }
}

Class B
{
   void Main()
   {
    A a = new A();
    Func( () => {a.Foo();});
   }

   void Func(Action onFinish)
   {
    //Enter your code here
    onFinish();
   }
+2

, :

namespace Assets
{
    public class Example
    {
        public Example GrabSomeFoodInTheFridge()
        {
            // some work
            return this;
        }

        public Example WatchTv()
        {
            // some work
            return this;
        }

        public Example EatFood()
        {
            // some work
            return this;
        }
    }

    public class Demo
    {
        public Demo()
        {
            var example = new Example();

            var instance = example
                .GrabSomeFoodInTheFridge()
                .EatFood()
                .WatchTv();
        }
    }
}

, , .

0

Source: https://habr.com/ru/post/1692276/


All Articles