Is there a way to call a generic function in a foreach loop for Types known at compile time? This code wont compile, because type is not a valid parameter for DoSmth, but illustrates what I want to do.
public class X { }
public class Y { }
public class Z { }
public class Example
{
public Example()
{
Types = new List<Type>
{
typeof(X),
typeof(Y),
typeof(Z)
};
}
private List<Type> Types { get; }
public void DoAllTypes()
{
foreach (var type in Types)
{
DoSmth<type>();
}
}
private void DoSmth<T>()
{
// ... do smth based on T
}
}
Edit: Why is the question a duplicate? I specifically stated "known at compile time". The other uses Reflections to get the types not known at compile time.