我有以下课程:
namespace MyClassLibrary
{
public class CalculatorGeneric<T> where T : IMyInterface
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public CalculatorGeneric()
{
Value1 = 100;
Value2 = 200;
}
public int Add(int val1, int val2)
{
Value1 = val1; Value2 = val2;
return val1 + Value2;
}
public int Multiply(int val1, int val2)
{
Value1 = val1; Value2 = val2;
return Value1 * Value2;
}
}
}
在代码的其他地方,我使用以下方法对解决方案中的所有方法进行预填充:
public static void PreJitAllMethods(Type type)
{
// get the type of all the methods within this instance
var methods = type.GetMethods(BindingFlags.DeclaredOnly |
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.Static);
if (type.IsGenericType)
{
// return;
}
// Jit all methods
foreach (var method in methods)
{
// jitting of the method happends here.
try
{
if (type.IsGenericType)
RuntimeHelpers.PrepareMethod(method.MethodHandle, new[] { typeof(object).TypeHandle });
else
RuntimeHelpers.PrepareMethod(method.MethodHandle);
}
catch (Exception ex)
{
}
}
}
没有where T : IMyInterface I
只能传递参数new[] { typeof(object).TypeHandle }
,但是我的问题是,我还需要为类方法调用调用RuntimeHelpers.PrepareMethod方法,其中类属参数包含例如where T: IMyInterface
。我该怎么办?