Generic Method Executed with a runtime type [duplicate]

I have the following code:

 public class ClassExample
{

    void DoSomthing<T>(string name, T value)
    {
        SendToDatabase(name, value);
    }

    public class ParameterType
    {
        public readonly string Name;
        public readonly Type DisplayType;
        public readonly string Value;

        public ParameterType(string name, Type type, string value)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");
            if (type == null)
                throw new ArgumentNullException("type");

            this.Name = name;
            this.DisplayType = type;
            this.Value = value;
        }
    }

    public void GetTypes()
    {
        List<ParameterType> l = report.GetParameterTypes();

        foreach (ParameterType p in l)
        {
            DoSomthing<p.DisplayType>(p.Name, (p.DisplayType)p.Value);
        }

    }
}

Now, I know I cannot perform DoSomething() is there any other way to use this function?


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.


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

Should work.


Generics types cannot be specified at runtime the way you'd like to here.

The simplest options would be to add a non-generic overload of DoSomething, or simply call DoSomething<object> and ignore p.DisplayType. Unless SendToDatabase depends on the compile-time type of value (and it probably shouldn't), there should be nothing wrong with giving it an object.

If you can't do those, you'll have to call DoSomething using reflection, and you'll take a big performance hit.


First we need to convert p.Value to the right type, since even if we know the type at compile time we can't pass the string straight to the method...

DoSomething<Int32>( "10" ); // Build error

For simple numeric types and DateTime, we can use

object convertedValue = Convert.ChangeType(p.Value, p.DisplayType);

Now we can use reflection to invoke the required generic method...

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

Strictly saying you can use MethodInfo.MakeGenericMethod for this.

But I recommend to change DoSomething to non-generic form instead, since it is not evident whether it really should be generic.