创建表达式< Func< T,K>>.在.Net Runtime中 [英] Create Expression<Func<T,K>> in .Net Runtime

查看:92
本文介绍了创建表达式< Func< T,K>>.在.Net Runtime中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存在一个名为LiteDB的不错的数据库.我发现不方便的是缺少属性来指定实体之间的关系类型(值/引用),尽管LiteDB提供了流畅的接口对其进行硬编码(详细信息:

there exists a nice database called LiteDB. What I find inconvenient is an absence of attributes for specifying the relation type (value/reference) between entities, though LiteDB provides fluent interface for hardcoding it (details: https://github.com/mbdavid/LiteDB/wiki/DbRef). I am lazy guy and don't want always update this hardcoded relations to follow the changes in my data model. So I decided to realize the runtime discovery of the data model entities with the properties attributed by DbRef (my custom attribute). Unfortunately, I am stuck a little with creating the

Expression<Func<T,K>> 

.net运行时中...,用于在以下调用(第一个参数)中提供它:

in the .Net runtime... for providing it in the following call (first parameter):

BsonMapper.Global.Entity<Order>().DbRef(x => x.Customer, "customers"); 

类型T和K在运行时作为System.Type的实例给出(此处示例:T-订单,K-客户).

Types T and K are given in runtime as instances of System.Type (here in example: T - Order, K - Customer).

如果你们能给我一些有关如何实例化的提示,我将不胜感激

I'll really appreciate if you guys give me some hints on how to instantiate

Expression<Func<T,K>> 

在.Net运行时中

以便将其提供给... DbRef(...)函数.

in .Net runtime in order to provide it to ...DbRef(...) function.

推荐答案

好,您具有实体类型T,属性类型K和属性名称.要构建Expression<Func<T, K>>,您可以简单地使用 Expression.Property Expression.Lambda 这样的方法:

Well, you have the entity type T, property type K and the property name. To build the Expression<Func<T, K>> you could simply use Expression.Parameter, Expression.Property and Expression.Lambda methods like this:

var parameter = Expression.Parameter(typeof(T), "x");
var body = Expression.Property(parameter, propertyName);
var selector = Expression.Lambda(body, parameter);

这篇关于创建表达式&lt; Func&lt; T,K&gt;&gt;.在.Net Runtime中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆