C# 带泛型T的DefineMethod
找不到类型或命名空间名称“T” 如何在参数中使用C# 带泛型T的DefineMethod,c#,cil,reflection.emit,C#,Cil,Reflection.emit,找不到类型或命名空间名称“T” 如何在参数中使用T Type[] tparams = { typeof(Expression<Func<T, object>>) }; MethodBuilder methodId = tbuilder.DefineMethod("Id", MethodAttributes.Public, typeof(IdentityPart), tparams); Type[]tparams={typeof(Expression)}; MethodB
T
Type[] tparams = { typeof(Expression<Func<T, object>>) };
MethodBuilder methodId = tbuilder.DefineMethod("Id", MethodAttributes.Public, typeof(IdentityPart), tparams);
Type[]tparams={typeof(Expression)};
MethodBuilder methodId=tbuilder.DefineMethod(“Id”,MethodAttributes.Public,typeof(IdentityPart),tparams);
代码必须是通用方法或类的一部分:
方法:
public void Method<T>()
{
// code snnipet
Type[] tparams = { typeof(Expression<Func<T, object>>) };
}
public class Class<T>
{
public void Method()
{
// code snnipet
Type[] tparams = { typeof(Expression<Func<T, object>>) };
}
}
public void方法()
{
//代码snnipet
类型[]tparams={typeof(表达式)};
}
类别:
public void Method<T>()
{
// code snnipet
Type[] tparams = { typeof(Expression<Func<T, object>>) };
}
public class Class<T>
{
public void Method()
{
// code snnipet
Type[] tparams = { typeof(Expression<Func<T, object>>) };
}
}
公共类
{
公开作废法()
{
//代码snnipet
类型[]tparams={typeof(表达式)};
}
}
您可以使用助手方法作为填写类型参数的工具。假设您希望T是一个名为MyVerySpecialType的类:
public static class Helper
{
public static Type[] TypeArrayReturnerWithGeneric<T>()
{
return new Type[] { typeof(Expression<Func<T, object>>) };
}
}
公共静态类帮助器
{
公共静态类型[]TypeArrayReturnerWithGeneric()
{
返回新类型[]{typeof(Expression)};
}
}
然后你可以做:
MethodBuilder methodId = tbuilder.DefineMethod("Id", MethodAttributes.Public, typeof(IdentityPart), Helper.TypeArrayReturnerWithGeneric<MyVerySpecialType>());
MethodBuilder methodId=tbuilder.DefineMethod(“Id”,MethodAttributes.Public,typeof(IdentityPart),Helper.TypeArrayReturnerWithGeneric());