Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带泛型T的DefineMethod_C#_Cil_Reflection.emit - Fatal编程技术网

C# 带泛型T的DefineMethod

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”

如何在参数中使用
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());