Impacts of new() contraint in C#

“Disecting the new() constraint” is very instructive: new() constraint has a big performance impact and we must know its implementation to understand why ctor exceptions are wrapped to a TargetInvocationException.

Dissecting the new() constraint in C#: a perfect example of a leaky abstraction – Dissecting the code

By the way, in same blog post, I’ve discovered that some well known static functions can be custom replaceable. It means you can locally redefined them. System.Activator.CreateInstance<T> is replaced by a faster one.

Beware, this is a completly not documented behavior than can be theoretically remove any time. Just to mitigate previous remark, this undocumented behavior is so important survived the Roslyn transition.

Advertisements

ExceptionDispatchInfo

Starting from .Net 4.5 (but I was not aware):

Starting from .NET 4.5 you can use ExceptionDispatchInfo class to rethrow an arbitrary exception object (an inner exception in this case) without altering the exception’s stack trace

via Dissecting the new() constraint in C#: a perfect example of a leaky abstraction – Dissecting the code

 

JSON Patch

I don’t know JsonPatch before reading this post from Ben Foster.

It’s a normalized way (RFC 6902) of specifying, in json format, patch to a json document. You write operations for add, removereplace, copy, move and test.

Main use is for reduce http request size (using http PATCH) but I think json is not necessarily the good format when request size matter (I like protobuf but I don’t know if a patch mechanism exist for it).

And in Ben Foster’s blog entry, I also discovers that .Net core already have an aspnet/JsonPatch (available for .Net 4.5.1). It’s easy to use in http server or outside. You creates a JsonPatchDocument with a list of operation, you can add yours then you apply them with a ApplyTo. Json.Net is used internally and we can add a custom IContractResolver.

Source: How to perform partial resource updates with JSON Patch and ASP.NET Core – Ben Foster