C
C#17mo ago
Ole

❔ Infer the second generic type from input data

I am wondering if its possible to write the following code in some way that it works like I want it to. My goal is to specify a class and an argument. The function should then create the class and pass the argument to it.
Execute<MyValidator0>(someData0); // Does not work, any way to change it so I can call it like this?
Execute<MyValidator1, MyDataType>(someData1); // Works


void Execute<V, Arg>(Arg argument) where V:IValidator<Arg>, new()
{
var instance = new V();
instance.ValidateAndThrow(argument);
}
Execute<MyValidator0>(someData0); // Does not work, any way to change it so I can call it like this?
Execute<MyValidator1, MyDataType>(someData1); // Works


void Execute<V, Arg>(Arg argument) where V:IValidator<Arg>, new()
{
var instance = new V();
instance.ValidateAndThrow(argument);
}
6 Replies
TheRanger
TheRanger17mo ago
ur method can only accept 2 generic arguments, create another one that accepts a single generic argument
Ole
Ole17mo ago
How would one do that when the first generic arguemtn requires the second? void Execute<v<arg>>(arg argument) is not valid from my understanding
TheRanger
TheRanger17mo ago
is there a reason u dont want to pass the 2nd generic argument?
Ole
Ole17mo ago
Partly to make it look neater, but I am also really curious if its possible
TheRanger
TheRanger17mo ago
not afaik
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts