项目中如下的泛型方法,因为要在运行时,动态指定类型参数,所以要利用反射来实现。
public static TR Deserialize<TR>(byte[] source, ref int offset)
一般做法如下:
// 变量 type是该方法所在类型的运行时Type
// model是已经定义的实例
MethodInfo genericMethod = type.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static);
MethodInfo mi = genericMethod.MakeGenericMethod(mode