Set The “NonSerializedAttribute” To An Auto Property

前端 未结 7 2359
青春惊慌失措
青春惊慌失措 2021-02-18 18:59

This cannot be done in C#. Any way to do it?

...

laugh, in case my little pun wasn\'t understood, what I mean is: how can I mark a property in C# as Non

7条回答
  •  北荒
    北荒 (楼主)
    2021-02-18 19:25

    You could probably do this with Mono.Cecil, a bytecode manipulation library. Theoretically, you could add custom attributes to the hidden backing field. This is so inconvenient, however, that I don't think it warrants an example.

    If you had a large application with your own postprocessor, you might consider creating your own substitute for NonSerializedAttribute that could be applied to properties. The postprocessor could then use Mono.Cecil or similar to apply NonSerializedAttribute to the backing fields. It's quite common for large applications to undergo such postprocessing to save that extra bit of typing.

提交回复
热议问题