I have a problem inserting a couple of lines of code in a property set

before my code it is an empy set (empty to simplify what i am doing wrong)

    public string Property1
        {
            get { return property1; }
            set
            {
            }
        }

After I want it to look like this

        public string Property1
        {
            get { return property1; }
            set
                        {
                            object before = Property1;
                OnPropertyChanged("Property1", before, value);
                        }
        }

my insertion code

        private void AddBeforeAfrerInvokerCall(PropertyDefinition
property, MethodBody setMethodBody)
        {
            var beforeVariable = new
VariableDefinition(MsCoreReferenceFinder.ObjectTypeReference);
            setMethodBody.Variables.Add(beforeVariable);

            cilWorker.InsertBefore(last, cilWorker.Create(OpCodes.Ldarg_0));
            cilWorker.InsertBefore(last,
cilWorker.Create(OpCodes.Call, property.GetMethod));
            cilWorker.InsertBefore(last, cilWorker.Create(OpCodes.Stloc_0));
            cilWorker.InsertBefore(last, cilWorker.Create(OpCodes.Ldarg_0));
            cilWorker.InsertBefore(last,
cilWorker.Create(OpCodes.Ldstr, property.Name));
            cilWorker.InsertBefore(last, cilWorker.Create(OpCodes.Ldloc_0));
            cilWorker.InsertBefore(last, cilWorker.Create(OpCodes.Ldloc_1));
            cilWorker.InsertBefore(last,
cilWorker.Create(OpCodes.Call,
OnPropertyChangedReference.MethodReference));
            cilWorker.InsertBefore(last, cilWorker.Create(OpCodes.Nop));
        }

So after my code runs i get this

.method public hidebysig specialname instance void
set_Property1(string 'value') cil managed
{
    .maxstack 4
    .locals (
        [0] object obj2)
    L_0000: nop
    L_0001: ldarg.0
    L_0002: call instance string
AssemblyToProcess.ClassWithBeforeAfterImplementation::get_Property1()
    L_0007: stloc.0
    L_0008: ldarg.0
    L_0009: ldstr "Property1"
    L_000e: ldloc.0
    L_000f: ldloc.1
    L_0010: call instance void
AssemblyToProcess.ClassWithBeforeAfterImplementation::OnPropertyChanged(string,
object, object)
    L_0015: nop
    L_0016: ret
}

Which looks the same as the decompiled version of the code i want

.method public hidebysig specialname instance void
set_Property1(string 'value') cil managed
{
    .maxstack 4
    .locals init (
        [0] object before)
    L_0000: nop
    L_0001: ldarg.0
    L_0002: call instance string
AssemblyToProcess.ClassWithBeforeAfterImplementation::get_Property1()
    L_0007: stloc.0
    L_0008: ldarg.0
    L_0009: ldstr "Property1"
    L_000e: ldloc.0
    L_000f: ldarg.1
    L_0010: call instance void
AssemblyToProcess.ClassWithBeforeAfterImplementation::OnPropertyChanged(string,
object, object)
    L_0015: nop
    L_0016: ret
}


But for some reason i get "System.InvalidProgramException : Common
Language Runtime detected an invalid program." when i try to load my
assembly.

I have attached the offending assembly.

Any help would be appreciated.

-- 
--
mono-cecil

Attachment: AssemblyExperiments.dll
Description: Binary data

Reply via email to