Frage

I found that the aspect constructor is invoked at compile-time, then the aspect instance is serialized and then it is deserialized at run-time.

Why this happens? Is it faster to perform a deserialization instead of invoking a constructor of the aspect?

War es hilfreich?

Lösung

The described process mainly allows you to initialize some of your aspect's fields during compile time, and then the deserialization will just restore the values each time the program runs. This can improve run-time performance if the fields computations are expensive - you can just run them once at compile time.

For example, you may want to build the format string of the logging aspect at compile time, as you already have info about method name, parameters, types.

The whole aspect life-cycle is described in detail on this page: http://doc.postsharp.net/content/aspect-lifetime

However, in cases when you do not need to do compile time initialization, it may be better to also avoid the serialization steps. PostSharp allows you to do that by using MsilAspectSerializer - this will actually generate instructions to invoke the constructor and set properties and fields.

This case is documented here: http://doc.postsharp.net/content/aspect-serialization

[OnMethodBoundaryAspectConfiguration(SerializerType=typeof(MsilAspectSerializer))]
public sealed MyAspect : OnMethodBoundaryAspect
{
    //
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top