C
C#2y ago
boom

✅ How do i pass a function with unknown parameters?

for instance
void WrapFunction()
{
return 1;
}

void WrapFunctionA(dynamic data);
void WrapFunctionB(int data);
void WrapFunctionC(string data);

void CallFunction(Func<?> passed_func) { passed_func(); }

CallFunction(WrapFunctionA);
CallFunction(WrapFunctionB);
void WrapFunction()
{
return 1;
}

void WrapFunctionA(dynamic data);
void WrapFunctionB(int data);
void WrapFunctionC(string data);

void CallFunction(Func<?> passed_func) { passed_func(); }

CallFunction(WrapFunctionA);
CallFunction(WrapFunctionB);
```
16 Replies
Anton
Anton2y ago
if the return type is void, it's an Action Func will always have a parameter, since it's by definition a delegate with a non-void return type oh you can't
boom
boomOP2y ago
what would be the closest method alike? basically, with more context
Anton
Anton2y ago
you either have to use MethodInfo, reflection checks, or multiple overloads, or wrap your delegates in a class
boom
boomOP2y ago
i have an "event bus", that once passed a <random> function, with unknown parameters it will execute without needing to know the parameters of given func
Anton
Anton2y ago
yeah the best way is to make an adapter class for that function or an adapter delegate that casts the argument to the expected type
boom
boomOP2y ago
the method i was going down, was having a separate class for the arguments, and using the "argument stack" for the functions to use
boom
boomOP2y ago
boom
boomOP2y ago
haven't fully written the class out yet, but the event will have a callback that's overrode later on is this sort of the method your referring to?
Anton
Anton2y ago
you'll have to box if you don't do generics throughout your whole event system so kinda like this
Action<object> Wrap<T>(Action<T> f) => (object arg) => f((T) arg);
Action<object> Wrap<T>(Action<T> f) => (object arg) => f((T) arg);
boom
boomOP2y ago
wtf am i seeing 😂 im not too familiar with c# syntax lol
Anton
Anton2y ago
but if you don't want to box then the approach eill be completely different
boom
boomOP2y ago
i also dont know what boxing is, sorry lol
Anton
Anton2y ago
Action<object> Wrap<T>(Action<T> f)
{
return (object arg) => f((T) arg);
}
Action<object> Wrap<T>(Action<T> f)
{
return (object arg) => f((T) arg);
}
like this, if it makes it easier on the eyes you can't make a generic event bus in C# that supports managed structs without boxing, even with unsafe, due to some unfortunate GC rules around references. It would only work with unmanaged structs + some unsafe things but with boxing, or if your event data are classes, the wrapper I just showed should do just fine this code isn't right tho, where's the only argument? you aren't passing anything in the end anyway, you might want something more straightforward look into the adapter pattern
boom
boomOP2y ago
just a bland representation. i think i got it figured out sort of. appreciate your help g
Anton
Anton2y ago
np
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server