I’ve been busy designing and building my company’s new product, so I haven’t had much time to write lately.

In a previous article I wrote, I went into depth regarding the various ways we could perform validation on method parameters using PostSharp. At the conclusion of that article, we ended up with a solution that gave us what we wanted: a way to designate individual parameters as needing validation at runtime simply by decorating each parameter with a single validation attribute.

Of course, that solution was the result of a wanton foray into the bowels of PostSharp, with the bowels in particular being that of theundocumented variety. So, hopefully no one was conducting themselves while being under a false pretext that our results were guaranteed to be stable. I know I wasn’t!

I have, since writing that article, happened to run into one particular side effect of the custom woven validation attribute worth some quick discussion.

Where’s mah ‘beforefieldinit’?

Indeed.

I noticed recently (whether it was due to us emitting some additional instructions in the static constructor or simply because of our use of a custom IAdvice implementation) that all classes containing any methods sporting parameters decorated by one of our validation attributes were being stripped of their beforefieldinit flags.

I won’t spend anyone’s precious time redundantly going over what exactly the beforefieldinit flag is (here’s a good source) but it’s certainly something our validation attributes shouldn’t be touching. Our advice does not depend on the order of initialization of any static fields other than its own in the classes it ends up getting applied to, so it is safe to say that our advice supports the beforefieldinit flag.

After poking around a bit in the SDK, I happened upon an interface eloquently titled IBeforeStaticConstructorAdvice, which basically allows us to express to the weaver whether or not our advice supports the beforefieldinit flag.

Adding Support for ‘beforefieldinit’

To get our beforefieldinit flag back, we simply need to replace the IAdvice interface being implemented by our base advice class with the IBeforeStaticConstructorAdvice interface (which itself implement IAdvice), and return true for the sole required method (not 100% if I posted the base class in the last article; should be enough to get the point across):

    public abstract class ValidationAdvice<TValidationAttribute>
      : IBeforeStaticConstructorAdvice, IDisposable
    {
      .
      .
      .
      public bool IsBeforeFieldInitSupport
      {
        get { return true; }
      }
      .
      .
      .
    }

Matt Weber

I'm the the Senior Software Architect at Emergingsoft where I lead the software development team. I am also the owner of this website. I enjoy well-designed code, independent thought, and the application of rationality in general. You can reach me at matt@badecho.com.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
   
© 2012-2013 Matt Weber. All Rights Reserved. Terms of Use.