Reputation: 25367
I want to be able to store function reference and disregard arguments until it actually is used.
Here's what I'd like for it to look like:
StoreType f=MyFunction;
.......
var r=f.Invoke(arg1,arg2,arg3) as ReturnType;
This is kind of like Action and Func, but those are strongly typed, and I want to be able to declare and use this type without precisely knowing how many arguments and of what types the function will take.
How do I do this in c#?
Upvotes: 4
Views: 336
Reputation: 748
You can use the older delegate
syntax.
public delegate ReturnType MyFunction(string arg1, int arg2, ...);
var result = MyFunction.Invoke(arg1, arg2, ...);
Upvotes: 1
Reputation: 1249
For the argument count, just pass an array of object containing the arguments.
f.Invoke(new object[]{ arg1, args2, args3, ... });
For the type use the method
Convert.ChangeType(objectToConvert, destinationType);
Should work for me :)
Upvotes: 1