I am trying to inject INotifyPropertyChanged code into a class.

the code is based on 
http://justinangel.net/AutomagicallyImplementingINotifyPropertyChanged
and i have checked it in here http://code.google.com/p/notifypropertyweaver/

Now I have most of it working but i cant seem to inject a call to a
method on the base class.
So here is my code for injecting the method call

        private static void
InjectMethodCallAfterPropertySet(PropertyDefinition property,
MethodDefinition onPropertyChanged)
        {
            var setMethodBody = property.SetMethod.Body;
            var cilWorker = setMethodBody.GetILProcessor();
            var ldarg0 = cilWorker.Create(OpCodes.Ldarg_0);
            var propertyName = cilWorker.Create(OpCodes.Ldstr,
property.Name);
            var callOnPropertyChanged = cilWorker.Create(OpCodes.Call,
onPropertyChanged);
            cilWorker.InsertBefore(setMethodBody.Instructions[0],
cilWorker.Create(OpCodes.Nop));
 
cilWorker.InsertBefore(setMethodBody.Instructions[setMethodBody.Instructions.Count
- 1], ldarg0);
            cilWorker.InsertAfter(ldarg0, propertyName);
            cilWorker.InsertAfter(propertyName,
callOnPropertyChanged);
            cilWorker.InsertAfter(callOnPropertyChanged,
cilWorker.Create(OpCodes.Nop));
        }

And my code for finding the base method returns the correct method

   public static MethodDefinition
GetOnPropertyChangedMethod(TypeDefinition type)
        {
            var method = (
                             from MethodDefinition x in type.Methods
                             where IsOnPropertyChangedMethod(x)
                             select x).FirstOrDefault();
            if (method != null)
            {
                return method;
            }
            var baseType = type.BaseType;
            if (baseType != null)
            {
                var typeDefinition =
TypeReferenceToTypeDefinition(baseType);
                return GetOnPropertyChangedMethod(typeDefinition);
            }
            return null;
        }

        private static TypeDefinition
TypeReferenceToTypeDefinition(TypeReference baseType)
        {
            var defaultAssemblyResolver = new
DefaultAssemblyResolver();
            var baseTypeAssembly =
defaultAssemblyResolver.Resolve(baseType.Scope as
AssemblyNameReference);
            return
baseTypeAssembly.MainModule.GetType(baseType.FullName);
        }

        private static bool IsOnPropertyChangedMethod(MethodDefinition
method)
        {
            return method.Name == "OnPropertyChanged"
                   && method.Parameters.Count == 1
                   && method.Parameters[0].ParameterType.FullName ==
"System.String";
        }


The resultant code looks like this in reflector

[Notify]
public string Property1
{
    [CompilerGenerated]
    get
    {
        return this.<Property1>k__BackingField;
    }
    [CompilerGenerated]
    set
    {
        this.<Property1>k__BackingField = value;
        base.PropertyChanged -= "Property1";
    }
}

which is obviously wrong.

So what am i doing wrong?

-- 
--
mono-cecil

Reply via email to