C
C#2y ago
E-slacker

❔ How to return a generic interface whose only parameter is determined on runtime?

So, I've got this code, it doesn't compile but it clarifies what I want to do:
private static IProblem<T>? GetProblem<T>(int day, T type)
{
try
{
var instance = Activator.CreateInstance(Problems[day]);
return (IProblem<T>?)instance;
}
catch (Exception e)
{
Console.Error.WriteLine(e);
return null;
}
}
private static IProblem<T>? GetProblem<T>(int day, T type)
{
try
{
var instance = Activator.CreateInstance(Problems[day]);
return (IProblem<T>?)instance;
}
catch (Exception e)
{
Console.Error.WriteLine(e);
return null;
}
}
The variable instance is of type object, I want it to be of IProblem<T> where T is a generic parameter of an interface that Problems[day] implements. I've been butting my head with this code for quite a while, but I can't find something that works. How could I accomplish this with generics? Or do I have to go dynamic?
17 Replies
Tinefol
Tinefol2y ago
Yoiu can't use new, need to specifically use Activator class?
Aaron
Aaron2y ago
what is the type of Problems
E-slacker
E-slackerOP2y ago
I can't use new with a Assembly that I get on runtime, can I? What I want to do, basically, is to get all classes of the project that implement IProblem<> and call some of their methods Type[] of different classes all implementing IProblem<>
Aaron
Aaron2y ago
what is the error you get with this block
E-slacker
E-slackerOP2y ago
InvalidCastException System.InvalidCastException: Unable to cast object of type 'Problems.Day1.Day1' to type 'Utils.IProblem`1[System.Type]
Aaron
Aaron2y ago
oh, it compiles, just doesn't run
E-slacker
E-slackerOP2y ago
Oh yeah my bad Day1 implements IProblem<int>
Aaron
Aaron2y ago
Day1 doesn't implement IProblem<Type> you gave it Type for T
E-slacker
E-slackerOP2y ago
Oh Given a Type object, how can i get its actual type?
Aaron
Aaron2y ago
to use as a generic? you can't, really is IProblem your interface?
E-slacker
E-slackerOP2y ago
Yes Should I just use dynamic?
Aaron
Aaron2y ago
could just get rid of the generic on IProblem, just using object instead dynamic would also probably work? no reason to use it if you don't have to
E-slacker
E-slackerOP2y ago
oh well, there's absolutely no way to use generics in this case?
Aaron
Aaron2y ago
not if you want to return it as an actual instance of IProblem
E-slacker
E-slackerOP2y ago
I see, thanks a lot for your time
cap5lut
cap5lut2y ago
i didnt touch it myself, but is this what u r looking for? https://learn.microsoft.com/en-us/dotnet/api/system.type.makegenerictype?view=net-7.0
Type.MakeGenericType(Type[]) Method (System)
Substitutes the elements of an array of types for the type parameters of the current generic type definition and returns a Type object representing the resulting constructed type.
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