type.getProperties收集和偶像之间的不同属性

发布于 2025-01-28 00:17:05 字数 898 浏览 2 评论 0原文

我正在尝试构建一个表达式树,

 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的集合属性,我声明它,因为

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

我可以在调用type.getProperty后获得项目属性。

我现在面临的问题是实体类是自动化的,并将其称为

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

我称之为type。

即使使用接口(例如Icollection),因为我无法更改类的属性类型,即使使用接口(例如Icollection)也有办法获取。任何帮助都将受到赞赏。

I am try to build an expression tree and I have this code

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

Now, Bar is a Collection property of Foo and I declare it as

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

I am able to get the Item property once I call the Type.GetProperty
enter image description here

The problem I'm facing now is that the Entity class is autogenerated and it is declared as

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

Once I call the Type.GetProperty on ICollection, the Item property is missing.
enter image description here

Is there a way to get that even when using Interface (e.g. ICollection) since I cannot change the type of the properties of the class. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2025-02-04 00:17:05

好吧,我现在明白了。

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

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

Ok I got it now.

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

I just need to convert the property type so I can access that property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文