0

suppose we have this scenario :

a class that you are not allowed to modify anything in it :

 public class ForbiddenClass_A
    {

        public void TheMethod()
        {
            //do stuff
        }
    }

and another read only class that calls a method from the previous class:

public class ForbiddenClass_B
{
    ForbiddenClass_A fc_a;

    void Update()
    {
        //some logic that if true it will call :
        fc_a.TheMethod();
    }
}

Now you have your class, that you do anything to it, and from it you want to know if TheMethod() :

public class MyClass
{
    //call this when TheMethod() from ForbiddenClass_A is called.
    public void TheMethod_Catcher()
    {

    }
}

Thank you!

alaslipknot
  • 551
  • 4
  • 8
  • 24
  • 2
    There are various ways of detecting the caller and then you can have `public void TheMethod() { TheMethod_Catcher(...caller...); //do stuff }; }` - but what would concern me is the idea of a 'forbidden class'. If it can't be changed, don't let it be changed. This sounds like an X-Y problem, what exactly are you trying to prevent? And what are you intending to actually do in `TheMethod_Catcher()`? – stuartd Oct 21 '18 at 22:44
  • 1
    I think, you should mark them 'internal' then they can only called from your code. – Poul Bak Oct 21 '18 at 22:44
  • 1
    @PoulBak internal isn't a guarantee of that. Private methods can be called by reflection, after all. – stuartd Oct 21 '18 at 22:45
  • 1
    You're right, there's no guarantee, but they would be hidden from IntelliSense and couldn't be found without reflection. – Poul Bak Oct 21 '18 at 22:47
  • @stuartd the "forbidden_class" is a bought asset (unity) and i don't want to modify them, if that's the ultimate solution, sure i'll just add an event subscription there, but i wanna know if there is another solution first. thanks :) – alaslipknot Oct 21 '18 at 22:49
  • @PoulBak could you please elaborate a little bit ? reflection kept popping up in all the results i found before posting this, but couldn't really found an exact solution, thanks – alaslipknot Oct 21 '18 at 22:51
  • 1
    Well, as @stuartd said, reflection can always be used to access even internal methods. – Poul Bak Oct 21 '18 at 22:53
  • @PoulBak i think stuartd solution would still require accessing the "forbidden" class to at least subscribe to an event – alaslipknot Oct 21 '18 at 23:01

1 Answers1

2

Is there a way to catch a method call without subscribing it to any sort of Events?

Decoupled messaging is probably where you want to be, event aggregator or any other pub sub method messaging system. Although you still have to subscribe to something, the participants need not know about each other allowing you to make the methods private.

Unity, MvvmLight both have these sorts of messaging systems, however they are truly dime-a-dozen, there are plenty

Example of how this might work

public CreateUserForm()
{
    InitializeComponent();
    EventPublisher.Instance.Subscribe<NewUserCreated>
        (n => listBoxUsers.Items.Add(n.User.Name));
}

...

// some other class
private void Update()
{

    var user = new User()
               {
                   Name = textBoxUserName.Text,
                   Password = textBoxPassword.Text,
                   Email = textBoxEmail.Text
               };
    EventPublisher.Instance.Publish(new NewUserRequested(user));
}

Update

There are injection techniques if you are interest for .net

Dynamically replace the contents of a C# method?

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • you said "Although you still have to subscribe to something", this mean that those "forbidden classes" still need to be modified right ? – alaslipknot Oct 21 '18 at 22:55
  • 2
    @alaslipknot indeed yes, in regards to release mode, unless you use some sort of Injection (which will be exceedingly hard), i doubt you can do this without modifying the target classes – TheGeneral Oct 21 '18 at 22:56