【问题标题】:How to access value of a property of an object within another property of the same obj in c#如何在c#中访问同一obj的另一个属性中的对象属性的值
【发布时间】:2016-10-03 08:40:48
【问题描述】:
    Mapper.Map<Foo, Bar>(
      o,
      new MyType{
        IntProp = service.GetIntProp(o.someProp),
        StrProp = IntProp == 1 ? "Hello" : "World"
      },
      someOptions => {}
    )

有没有办法只访问IntProp 的值而不创建一个变量来保存MyType?我可以再次调用该服务,但我有点担心性能。

【问题讨论】:

  • 为什么不直接创建变量?

标签: c# automapper


【解决方案1】:

字段初始值设定项不能引用非静态字段、方法或属性。 有关详细信息,请参阅此问题:Can properties inside an object initializer reference each other?

【讨论】:

    猜你喜欢
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    相关资源
    最近更新 更多