0

I have an abstract class for Lua scripts. I have a method called Expose which registers a function to the Lua environment.

protected void Expose(string name, MethodBase method)
    {
        this.Lua.RegisterFunction(name, this, method);
    }

However, I want to make it easier by passing the method name only instead of doing this:

this.Expose(this.GetType().GetMethod(...

I want to be able to do this:

this.Expose(LuaExports.DoSomething);

So instead of passing MethodBase, what do I need to pass? Note that the passed argument can be a method that returns something, or a method that does not return anything.

Gilbert Williams
  • 970
  • 2
  • 10
  • 24
  • 3
    possible duplicate of [Pass Method as Parameter using C#](http://stackoverflow.com/questions/2082615/pass-method-as-parameter-using-c-sharp) – DrewJordan Jul 14 '15 at 13:38
  • Why not pas the delegate? – Silvermind Jul 14 '15 at 13:38
  • 1
    @GilbertWilliams take it easy... just trying to help. I'm pretty sure you can't pass a single delegate that does both, which is why I think this is a duplicate. – DrewJordan Jul 14 '15 at 13:44
  • @DrewJordan Sorry, I just hate when people here immiediately mark posts as duplicates. – Gilbert Williams Jul 14 '15 at 13:45
  • np. Sometimes it isn't, and sometimes it is... that's why my flag is marked as a *possible* duplicate, and needs some others to agree before being officially marked as such. Wouldn't be the first time I was wrong. – DrewJordan Jul 14 '15 at 13:48

2 Answers2

4

You need two methods, one that accepts an Action delegate (a void method) and the other that accepts aFunc<T> delegate (a method that returns T):

protected void Expose(string name, Action method)
{
    method(); // will invoke the method passed.
}

protected void Expose(string name, Func<SomeType> method)
{
    var value = method(); // will invoke the method passed and assign return result to value.
}
David Arno
  • 42,717
  • 16
  • 86
  • 131
  • What if I don't know the type of the Func? Also, how do I convert them to MethodBase? Lua.RegisterFunction takes MethodBase as parameter... – Gilbert Williams Jul 14 '15 at 13:43
  • use 'object' or something... the solution that David gave you is able if you declare the class using the 'T' as generic type. – Mauro Bilotti Jul 14 '15 at 13:45
  • @GilbertWilliams, how can you not know it? Do you use the return value? If not, just use the first option and use `() => method()` as the parameter. That way your method with a return gets called via an `Action` lambda and you only need that version. – David Arno Jul 14 '15 at 13:46
0

The closest I believe you can come is passing a delegate type directly:

protected void Expose(string name, Delegate delegate)
{
    this.Lua.RegisterFunction(name, d.Target, d.Method);
}

Although to be able to call this, you would have to create a delegate type, which can be done with a cast:

 this.Expose("Name", (Action)LuaExports.DoSomething);

If you don't want to have the cast you would have to write a whole bunch of genetic overloads of Expose that each take a separate delegate type:

Expose(string name, Action action);
Expose<T1>(string name, Action<T1> action);
Expose<T1, T2>(string name, Action<T1, T2> action);
...
Expose<T1, TResult>>(string name, Func<T1, TResult> action);
Expose<T1, T2, TResult>>(string name, Action<T1, T2, TResult>> action);
...
shf301
  • 31,086
  • 2
  • 52
  • 86