c# reflection in generic method code example
Example 1: c# reflection create generic type
Type generic = typeof(Dictionary<,>);
Type[] typeArgs = { typeof(string), typeof(Test) };
Type constructed = generic.MakeGenericType(typeArgs);
var instance = Activator.CreateInstance(constructedType);
Example 2: generic method c#
static void Swap<T>(ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
public static void TestSwap()
{
int a = 1;
int b = 2;
Swap<int>(ref a, ref b);
System.Console.WriteLine(a + " " + b);
}