创建表达式<func <t,k>&gt;在.Net运行时

时间:2016-10-13 16:23:39

标签: c# .net reflection mapping runtime

存在一个名为LiteDB的漂亮数据库。我觉得不方便的是缺少用于指定实体之间关系类型(值/引用)的属性,尽管LiteDB提供了用于硬编码的流畅接口(详情:https://github.com/mbdavid/LiteDB/wiki/DbRef)。我是懒人,不想总是更新这个硬编码关系来跟踪我的数据模型中的变化。所以我决定用DbRef(我的自定义属性)归属的属性来实现数据模型实体的运行时发现。不幸的是,我因创建

而陷入困境
type
  TForm1 = class(TForm)
    StringGrid: TStringGrid;
    procedure StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    arrState: array[1..4, 1..4] of Integer;
end;

procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  iRow, iCol: Integer;
  arrk: array[1..4, 1..4] of Integer;
begin
  for iCol := 4 downto 1 do
  begin
    for iRow := 4 downto 1 do
    begin
      if (gdSelected in State) then
      begin
        case arrState[ARow, aCol] of
           0: begin
             StringGrid.Canvas.Brush.Color := clWhite;
             Rect := StringGrid.CellRect(iCol, iRow);
             StringGrid.Canvas.FillRect(Rect);
             Inc(arrState[iRow, iCol]);
           end;

           1: begin
             StringGrid.Canvas.Brush.Color := clRed;
             Rect := StringGrid.CellRect(iCol, iRow);
             StringGrid.Canvas.FillRect(Rect);
             Inc(arrState[iRow, iCol]);
           end;

           2: begin
             StringGrid.Canvas.Brush.Color := $008CFF;
             Rect := StringGrid.CellRect(iCol, iRow);
             StringGrid.Canvas.FillRect(Rect);
             Inc(arrState[iRow, iCol]);
           end;

           3: begin
             StringGrid.Canvas.Brush.Color := clGreen;
             Rect := StringGrid.CellRect(iCol, iRow);
             StringGrid.Canvas.FillRect(Rect);
             arrState[iRow, iCol] := 0;
           end;

         end;
       end;
     end;
   end;
 end;
<。>在.Net运行时...用于在以下调用中提供它(第一个参数):

Expression<Func<T,K>> 

类型T和K在运行时作为System.Type的实例给出(例如:T - Order,K - Customer)。

如果你们给我一些关于如何实例化

的提示,我将非常感激
BsonMapper.Global.Entity<Order>().DbRef(x => x.Customer, "customers"); 
<。>在.Net运行时中,以便将其提供给... DbRef(...)函数。

2 个答案:

答案 0 :(得分:1)

嗯,您有实体类型T,属性类型K和属性名称。要构建Expression<Func<T, K>>,您只需使用Expression.ParameterExpression.PropertyExpression.Lambda这样的方法:

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

答案 1 :(得分:-1)

从你的问题。让我给你发一个截图,也许它可以给你一个线索Expression> Example

public IEnumerable<TEntity> Fetch(Expression<Func<TEntity, bool>>  predicate, Func<IQueryable<TEntity>,
        IOrderedQueryable<TEntity>> orderBy =null, int? page = null, int? pageSize = null)
    {
        IQueryable<TEntity> query = _dbSet;

        if (orderBy != null)
        {
            query = orderBy(query);
        }
        if (predicate != null)
        {
            query = query.AsExpandable().Where(predicate);
        }
        if (page != null && pageSize != null)
        {
            query = query.Skip((page.Value - 1) * pageSize.Value).Take(pageSize.Value);
        }
        return query;
    }

我希望这会有所帮助

相关问题