-1

I am developing several plugin methods that generate a string, and pass it back to my main method. From here, I can then come up with a sequence number. However, I want to avoid integrating the plugin into my main program - basically anything that forces me to change my main program in any way to recognise this plugin is off-limits.

So - in my main program, I generate a string, then match it with other matching strings to figure out the sequence number. The trouble is that I need to know the format of this string so I know where to input this number.

I was thinking about possibly passing in a method that knows the matching functions for this string, but I'm not sure how I can pass a method from one program to another.

I have found this question, but this only appears to be within the same program.

Community
  • 1
  • 1
Ben
  • 2,433
  • 5
  • 39
  • 69

2 Answers2

0

In C#, you can either pass a method as the argument of another method provided that it returns something which the recipient method is expecting. Thus, given this:

public int Foo(){ ... }


public void Bar(int i) { ... }

You can do something like so: ...Bar(this.Foo());

However, I think that what you are after actually are delegates, which are essentially function pointers. This would allow you to call the method to which the delegate points to from some other method.

As a side note, you could consider looking into the Strategy Design Pattern. This pattern allows you to essentially determine which behaviour (logic) to call at run time.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • Your example is passing the return value of a method call as argument, not the method itself. You may want to read up on `Action<>` and `Func<>` (they have been added to the language after `delegate`s were introduced). – Pieter Witvoet Oct 01 '15 at 07:20
  • @PieterWitvoet: One of the examples yes, but I also clarified on delegates. Don't functions resolve to delegates? – npinti Oct 01 '15 at 08:13
  • You're right, they're very similar. I find `Action<>` and `Func<>` more convenient in practice, specifically because they don't require delegate declarations. Their main limitation is that you can't use `ref` and `out` parameters however. As for the example, given that the question is about how to pass methods as parameters, an example using delegates would make more sense if you ask me. – Pieter Witvoet Oct 01 '15 at 19:51
0

First of all for your case you dont need to pass the method. You can make the method public and access in another program in same namespace like:- ClassObject.Method();... here object should be of class where your method is defined

You can Contain the Return value of function as a parameter while calling if thats what you are trying(like:- testapp(Square(x)) if the type of param is same as of method's Return Type .. In another Case Delegates could be useful.

CThakur
  • 136
  • 8