Search code examples
postsharp

PostSharp and uninitialized objects


Assumed I have an aspect implementing IInstanceScopedAspect and I have this aspect applied to methods in a type. How can I initialize the aspects when creating the object with FormatterServices.GetUninitializedObject? The constructor is not executed and therefore I get a NullReferenceException when I execute the method where the aspect is applied.

Is there a PostSharp API which can be used to initialize the object's aspects ?


Solution

  • There is a InitializeCurrentAspects method available for exactly this scenario, as documented here; http://doc.sharpcrafters.com/postsharp-2.0/Default.aspx##PostSharp-2.0.chm/html/M_PostSharp_Aspects_AspectUtilities_InitializeCurrentAspects.htm