Skip to content

Instantly share code, notes, and snippets.

@mishrsud
Created February 27, 2018 06:04
Show Gist options
  • Select an option

  • Save mishrsud/d080307ad02f4f935deb7ddf7f3b9fd3 to your computer and use it in GitHub Desktop.

Select an option

Save mishrsud/d080307ad02f4f935deb7ddf7f3b9fd3 to your computer and use it in GitHub Desktop.
void Main()
{
var str = "Hello";
var integer = 23;
System.Reflection.MethodInfo methodDefinition = typeof(GenericClass).GetMethod("PrintName");
System.Reflection.MethodInfo method = methodDefinition.MakeGenericMethod(integer.GetType());
method.Invoke(new GenericClass(), new object [] {integer});
}
// Define other methods and classes here
public class GenericClass
{
public void PrintName<T>(T param)
{
System.Console.WriteLine($"Called with type: {typeof(T).Name}, value: {param}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment