Make names of named tuples appear in serialized JSON responses

后端 未结 4 1636
借酒劲吻你
借酒劲吻你 2021-02-05 05:31

Situation: I have multiple Web service API calls that deliver object structures. Currently, I declare explicit types to bind those object structures together. F

4条回答
  •  [愿得一人]
    2021-02-05 06:00

    Problem with using named tuples in your case is that they are just syntactic sugar.

    If you check named-and-unnamed-tuples documentation you will find part:

    These synonyms are handled by the compiler and the language so that you can use named tuples effectively. IDEs and editors can read these semantic names using the Roslyn APIs. You can reference the elements of a named tuple by those semantic names anywhere in the same assembly. The compiler replaces the names you've defined with Item* equivalents when generating the compiled output. The compiled Microsoft Intermediate Language (MSIL) does not include the names you've given these elements.

    So you have problem as you do your serialization during runtime, not during compilation and you would like to use the information which was lost during compilation. One could design custom serializer which gets initialized with some code before compilation to remember named tuple names but I guess such complication is too much for this example.

提交回复
热议问题