Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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/2/.net/25.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# 如何将PropertyInfo转换为属性表达式并使用它调用泛型方法?_C#_.net_Linq_System.reflection - Fatal编程技术网

C# 如何将PropertyInfo转换为属性表达式并使用它调用泛型方法?

C# 如何将PropertyInfo转换为属性表达式并使用它调用泛型方法?,c#,.net,linq,system.reflection,C#,.net,Linq,System.reflection,如何转换为可用于调用方法的属性表达式 我试图使用来构造表达式,但当我将此表达式用作propertyExpression参数时,出现以下错误: 无法从用法推断方法的类型参数。尝试显式指定类型参数。 这个错误可能是指TProperty类型参数,我不知道如何指定它只有PropertyInfo 我这样做是为了: 更新 不工作的代码: var propertyInfo = typeof(Foo).GetProperties()[0]; var expression = Expression.Default

如何转换为可用于调用方法的属性表达式

我试图使用来构造表达式,但当我将此表达式用作
propertyExpression
参数时,出现以下错误:

无法从用法推断方法的类型参数。尝试显式指定类型参数。

这个错误可能是指
TProperty
类型参数,我不知道如何指定它只有
PropertyInfo

我这样做是为了:

更新

不工作的代码:

var propertyInfo = typeof(Foo).GetProperties()[0];
var expression = Expression.Default(typeof(Foo));
var expressionProperty = Expression.Property(expression, propertyInfo);
Ignore(expressionProperty);

t属性
仅存在于c#源代码文本中。编译器总是将其解析为具体类型。如果你有办法

void Test<T>(T arg)
{
}
编译器为两种方法生成代码

void Test(string arg)
{
}

void Test(int arg)
{
}

这意味着,如果您想拥有一个可调用的方法,就必须为泛型参数提供具体类型。

属性表达式要求对特定对象进行属性访问。这里有几个选择。首先,如果这是在一个实体对象内完成的,则可以简单地使用ConstantPression来构建属性表达式:

// Already have PropertyInfo in propInfo
Expression.Property(Expression.Constant(this, this.GetType()), propInfo)
ParameterExpression pe = Parameter.Expression(typeof(MyEntity), "eParam");
Expression propExp = Expression.Property(pe, propInfo);
但是,由于您需要一个
表达式
,因此似乎必须使用ParameterExpression来构建它:

// Already have PropertyInfo in propInfo
Expression.Property(Expression.Constant(this, this.GetType()), propInfo)
ParameterExpression pe = Parameter.Expression(typeof(MyEntity), "eParam");
Expression propExp = Expression.Property(pe, propInfo);
然而,这里的踢球者。。。这只是一个简单的表达。要转换为所需的表达式,需要使用
expression.Lambda
获取所需类型的Func表达式。问题出在哪里?您不知道用于定义lambda表达式的泛型参数的属性的类型

Expression<Func<MyEntity, ????>> eFunc = Expression.Lambda<Func<MyEntity, ????>>(propExp, pe);
Expression eFunc=Expression.Lambda(propExp,pe);
这是这样做问题的关键。这并不是说这是不可能的。。。只是用这种方法是行不通的。您必须使用一些运行时和静态类型技巧(以及明智地使用操作而不是Funcs)才能使其正常工作。

var entityType=propertyInfo.DeclaringType;
var entityType = propertyInfo.DeclaringType;
var parameter = Expression.Parameter(entityType, "entity");
var property = Expression.Property(parameter, propertyInfo);
var funcType = typeof(Func<,>).MakeGenericType(entityType, propertyInfo.PropertyType);
var lambda = Expression.Lambda(funcType, property, parameter);

structureConfiguration.GetType()
   .GetMethod("Ignore")
   .MakeGenericMethod(propertyInfo.PropertyType)
   .Invoke(structureConfiguration, new[]{lambda});
var参数=Expression.parameter(entityType,“实体”); var property=Expression.property(参数,propertyInfo); var funcType=typeof(Func).MakeGenericType(entityType,propertyInfo.PropertyType); var lambda=Expression.lambda(funcType,property,parameter); structureConfiguration.GetType() .GetMethod(“忽略”) .MakeGenericMethod(propertyInfo.PropertyType) .Invoke(structureConfiguration,new[]{lambda});
您应该显示不起作用的代码…尝试过,但获取的
类型或方法有1个泛型参数,但提供了2个泛型参数。必须为每个泛型参数提供泛型参数。
错误。StackTrace:at System.RuntimeType.SanityCheckGenericArguments(RuntimeType[]genericArguments,RuntimeType[]GenericParameters)at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[]methodInstantiation)在这里是当我注释
.MakeGenericMethod(…)时的代码(DbModelBuilder modelBuilder modelBuilder)
我得到了
不能对ContainsGenericParameters为true的类型或方法执行后期绑定操作。
结果是
funcType
不是必需的。