访问`Expression <func <typea,typeb >>`中的TypeA属性

时间:2019-01-14 16:13:57

标签: c#

访问% Create sphere figure1 = figure; axes1 = axes('Parent',figure1); hold(axes1,'on'); sphere; view(axes1, 3); % create a hidden copy of the axis axesCopy = copyobj(axes1, figure1); % sync axes on 3d rotation Link = linkprop([axes1, axesCopy],{'CameraUpVector', 'CameraPosition', 'CameraTarget', 'XLim', 'YLim', 'ZLim'}); setappdata(figure1, 'StoreTheLink', Link); rotate3d on set(axes1, 'Visible','off', 'Color','none', ... 'XLimMode','auto', 'YLimMode','auto', 'ZLimMode','auto') set(axesCopy, ... 'XLimMode','manual', 'YLimMode','manual', 'ZLimMode','manual') delete(get(axesCopy, 'Children')) grid(axesCopy,'on'); uistack(axesCopy, 'bottom') % respond to changes in XTick and YTick axis properties addlistener(figure1,'WindowMouseRelease', @(src, evnt) onTickChange(axes1, axesCopy)); addlistener(figure1,'SizeChanged', @(src, evnt) onTickChange(axes1, axesCopy)); onTickChange(axes1, axesCopy); %% functions function onTickChange(axes, axesCopy) zticks_orig = zticks(axes); ZTickLabels = num2str(zticks_orig(:), '%.2f ($)'); % update ticks set(axesCopy, 'ZTick', zticks(axes), 'ZTickLabel', ZTickLabels); drawnow; end 中的TypeA属性。

在从TypeA到TypeB的以下ProjectorAB类中。我想像使用Expression<Func<TypeA,TypeB>>一样访问TypeA属性。

return t => new {...}

1 个答案:

答案 0 :(得分:0)

创建Expression<>的通常方法是从lambda箭头=>隐式转换为相关的Expression<>。但是=>箭头的右侧大部分是“简单”表达式,而不是块{ … }

如果没有Expression<>,则可以创建一个简单的Func<>委托实例,如下所示:

public static Func<TypeA, TypeB> Projection
{
  get
  {   
    return someTypeA =>
    {
      var tInfo = new TypeB_SubInfo
      {
        prop1 = someTypeA.Whatever,
        // ...
        prop5 = someTypeA.Foobar,
      };

      var tItems = new List<TypeB_SubItem>()
      {                    
      };

      return new TypeB
      {
        Items = tItems,
        Info = tInfo,
      };
    };
  }
}

当您想要一个表达式树时,您必须重写为以下内容:

public static Expression<Func<TypeA, TypeB>> Projection
{
  get
  {   
    return someTypeA => new TypeB
    {
      Info = new TypeB_SubInfo
      {
        prop1 = someTypeA.Whatever,
        // ...
        prop5 = someTypeA.Foobar,
      },

      Items = new List<TypeB_SubItem>()
      {                    
      },
    };
  }
}