繁体   English   中英

Type.GetProperties Collection 和 ICollection 之间的不同属性

[英]Type.GetProperties different properties between Collection and ICollection

我正在尝试构建一个表达式树,我有这个代码

 var param = Expression.Parameter(typeof(T), "Foo");
 var property = Expression.Property(param, "Bar");
 var param2 = Expression.Parameter(property.Type.GetProperty("Item").PropertyType, "x");

现在, Bar 是 Foo 的 Collection 属性,我将其声明为

public Collection<Bar> Bar {get;set;}

调用 Type.GetProperty 后,我可以获取 Item 属性在此处输入图像描述

我现在面临的问题是 Entity 类是自动生成的,它被声明为

public ICollection<Bar> Bar {get;set;}

一旦我在 ICollection 上调用 Type.GetProperty,就会丢失 Item 属性。 在此处输入图像描述

即使使用接口(例如ICollection),有没有办法得到它,因为我无法更改类属性的类型。 任何帮助表示赞赏。

好的,我现在明白了。

var converted = Expression.Convert(property, typeof(Collection<Bar>));
var param2 = Expression.Parameter(converted.Type.GetProperty("Item").PropertyType, "x");

我只需要转换属性类型,以便我可以访问该属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM