'C# Call a generic method while iterating over a list of generic types [duplicate]

I want to call a generic method inside a loop. The iterator would contain all the Classes that need to be used with the generic method:

    public void DoSomething<T>()
    {
    }

    public void DoSomethingWithAListOfT()
    {
        List<Type> list = new();
        
        list.Add(typeof(string));
        list.Add(typeof(bool));

        foreach (var typeToPass in list)
        {
            DoSomething<typeToPass>(); //Do not compile
        }
    }

I have seen some references about MakeGenericType but this seems useful to instantiate a concrete object from a list of generics, I do not see it applicable to my case.

Any way to do this ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source