I need to store the Parameter<T1>
returned by Build()
as a Parameter<object>
(because I'm storing the parameters in an array, the other way is just wayyyy too much copy-pasting the same class for each amount of parameters, since c# doesn't have variadic generics).
The problem is the cast (Parameter<object>) (object) (/* value of type Parameter<int> */);
, since int
isn't castable to object
. How do I solve this issue?
I have the following monstrosity (imagine T1
is int
):
public static IEventBuilder<(IOrigin origin, T1 arg1, T2 arg2)>
Params<T1, T2>(
this IEventBuilder<(IOrigin origin, string msg)> eventBuilder,
Func<IParameterBuilder<string>, IParameterBuilder<T1>> param1,
Func<IParameterBuilder<string>, IParameterBuilder<T2>> param2)
{
return new ParamsBuilder<(IOrigin, T1, T2)>(
eventBuilder,
(origin, objs) => (origin, (T1) objs[0], (T2) objs[1]), // ignore this line
(Parameter<object>) (object) param1(new RootParameterBuilder()).Build(), // Build() returns Parameter<T1> (e.g. int)
(Parameter<object>) (object) param2(new RootParameterBuilder()).Build());
}
Not really important, but in case you need a bit of context, here's some example usage of Params
:
_dispatcher.On // IEventBuilder<IEvent>
.Chat() // IEventBuilder<(IOrigin, string)>
.Params( // IEventBuilder<(IOrigin, double)>
p => p // IParameterBuilder<string>
.Transform(Convert.ToInt32) // IParameterBuilder<int>
.Transform(i => i + 128.0)) // IParameterBuilder<double>
.Invoke(t =>
{
// t is (IOrigin, double)
});