Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read and set private fields without a performance hit in c#?

RavenDB can serialize all public, private and protected properties on any given C# object, even without any serialization attributes.

I know that this can be done with reflection, but that would have some performance issues. Are there other more efficient ways to achieve this?

To use the built-in serialization in .NET requires annotations or implementing some serialization interfaces, whereas RavenDB can serialize any given object to and from JSON.

I'm more interested in technology it uses and not RavenDB in specific.

like image 237
Olsenius Avatar asked Nov 13 '22 03:11

Olsenius


1 Answers

If setting properties through reflection is too slow, you can generate dynamic methods that set them at runtime. This can be done using Relection.Emit or by creating an compiling an Expression<T>.

like image 200
CodesInChaos Avatar answered Dec 25 '22 23:12

CodesInChaos