Debugging Pain of INotifyPropertyChanged with Mono Cecil

December 8, 2010

So I officially hate my life!  I just lost a day of my life trying to get NotifyPropertyChanged weaving working with Mono.Cecil.  It worked brilliantly in .Net, but blew up in Silverlight with a VerificationException (“Operation could destabilize the runtime”) as soon as I tried to invoke my rewritten Property Setters.

I literally combed through every IL instruction in both Reflector and ILDASM and verified everything was as it should be.  In the end the issue was a peculiarity with the CoreCLR as oposed to the full blown CLR.  The CoreCLR requires all locals to be flagged as init even if play by the rules and assign to the local before reading from it.

Here’s the offending IL:

.method public hidebysig specialname instance void set_Property(string 'value') cil managed
    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor()
    .maxstack 2
    .locals init (
        [0] string str,
        [1] bool flag)
    L_0000: nop 
    L_0001: ldarg.0 
    L_0002: call instance string SynerG.Weaving.Tests.DecoratedClass::get_Property()
    L_0007: stloc.0 
    L_0008: ldarg.0 
    L_0009: ldarg.1 
    L_000a: stfld string SynerG.Weaving.Tests.DecoratedClass::<Property>k__BackingField
    L_000f: ldarg.1 
    L_0010: ldloc.0 
    L_0011: call bool [mscorlib]System.Object::Equals(object, object)
    L_0016: stloc.1 
    L_0017: ldloc.1 
    L_0018: brtrue.s L_0028
    L_001a: nop 
    L_001b: ldarg.0 
    L_001c: ldstr "Property"
    L_0021: callvirt instance void [SynerG.Silverlight]SynerG.Notification.NotifyingBase::OnPropertyChanged(string)
    L_0026: nop 
    L_0027: nop 
    L_0028: ret 

 You can fix this with a cheeky:

methodBody.InitLocals = true;

Phew.  That’s a day of my life that I’m never going to get back again!


5 Responses to “Debugging Pain of INotifyPropertyChanged with Mono Cecil”

  1. Simon Cropp said


    I had exactly the same issue when writing

    To mitigate against these issues I now run peverify as part of my unit tests

    If u have any more similar issues a quick post on is usually the quickest way to solve it

    • johnhmarks said

      Thanks for the tip Simon. I’ll certainly do this.

      I must say that overall I’ve been very impressed with the quality of Cecil. It’s much cleaner than CCI.

  2. Jb Evain said

    Yeah that’s annoying. I should probably make that the default, considering that 99% of the time that’s what we want to emit.

  3. David Turczi said

    Hi! Your blog just saved my life TWICE 🙂 So thanks are in order…

    But, can you help me: where has my silverlight debug went with all this IL weaving? It says no symbols has been loaded for this document – for all .cs in my SL project… (Even if I uncheck “require all files to exact match”)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: