如何转换的PropertyInfo财产EX pression,并用它来调用泛型方法? [英] How to convert PropertyInfo to property expression and use it to invoke generic method?
问题描述
如何转换<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx"><$c$c>PropertyInfo$c$c>到特性ex pression可用于调用<一href="http://msdn.microsoft.com/en-us/library/gg671225%28v=vs.103%29.aspx"><$c$c>StructuralTypeConfiguration<TStructuralType>.Ignore<TProperty>(Ex$p$pssion<Func<TStructuralType, TProperty&GT;&GT; propertyEx pression) 方法?
我试图用 防爆pression.Property()
建设前pression,但我得到的时候我用这个EX pression为 propertyEx pression
参数:
的方法的类型参数不能从使用推断。请尝试显式指定类型参数。
此错误可能是指 TProperty
键入参数,我不知道如何指定只具有的PropertyInfo
。
我做这涉及到:<一href="http://stackoverflow.com/questions/9585263/use-entity-frameworks-structuraltypeconfiguration-ignore-to-ignore-all-proper">Use实体框架的StructuralTypeConfiguration.Ignore()忽略所有属性,但指定的一组。
更新
code这是不工作:
VAR的PropertyInfo = typeof运算(富).GetProperties()[0];
VAR EX pression =前pression.Default(typeof运算(富));
VAR EX pressionProperty =前pression.Property(如pression,的PropertyInfo);
忽略(如pressionProperty);
VAR参数=前pression.Parameter(的EntityType,实体);
VAR属性=前pression.Property(参数的PropertyInfo);
VAR funcType = typeof运算(Func键&LT;,&GT;)MakeGenericType(的EntityType,propertyInfo.PropertyType)。
VAR的λ=前pression.Lambda(funcType,财产,参数);
structureConfiguration.GetType()
.GetMethod(忽略)
.MakeGenericMethod(propertyInfo.PropertyType)
.Invoke(structureConfiguration,新的[] {拉姆达});
How to convert PropertyInfo
to property expression which can be used to invoke StructuralTypeConfiguration<TStructuralType>.Ignore<TProperty>(Expression<Func<TStructuralType, TProperty>> propertyExpression)
method?
I tried to use Expression.Property()
to construct expression but I am getting following error when I use this expression as propertyExpression
parameter:
The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly.
This error probably refers to TProperty
type parameter which I don't know how to specify having only PropertyInfo
.
I am doing this in relation to: Use Entity Framework's StructuralTypeConfiguration.Ignore() to Ignore all properties but specified set.
UPDATE
Code which is not working:
var propertyInfo = typeof(Foo).GetProperties()[0];
var expression = Expression.Default(typeof(Foo));
var expressionProperty = Expression.Property(expression, propertyInfo);
Ignore(expressionProperty);
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});
这篇关于如何转换的PropertyInfo财产EX pression,并用它来调用泛型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!