Return Anonymous Type from a function

前端 未结 6 997
庸人自扰
庸人自扰 2020-12-16 11:05

Can I use an anonymous type as a return type in a Function, and then stuff that returned value into an array or collection of some sort whilst also adding an additional fiel

6条回答
  •  自闭症患者
    2020-12-16 12:03

    This is a very popular question. In general you cannot return an anonymous type due to the requirement of strong typing. However there are a couple of workarounds.

    1. Create a simple type to represent the return value. (See here and here). Make it simple by generating from usage.
    2. Create a helper method to cast to the anonymous type using a sample instance for casting.

提交回复
热议问题