Generic Method Executed with a runtime type [duplicate]

You can, but it involves reflection, but you can do it.

typeof(ClassExample)
    .GetMethod("DoSomething")
    .MakeGenericMethod(p.DisplayType)
    .Invoke(this, new object[] { p.Name, p.Value });

This will look at the top of the containing class, get the method info, create a generic method with the appropriate type, then you can call Invoke on it.

Leave a Comment