private FieldDefinition AddPropertyFieldDefinition(PropertyDefinition property) { string fieldName = $"{property.Name}Property"; var declaringType = property.DeclaringType; var fieldDefinition = new FieldDefinition(fieldName, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.InitOnly, _catelType.PropertyDataType); declaringType.Fields.Add(fieldDefinition); fieldDefinition.MarkAsCompilerGenerated(_msCoreReferenceFinder); return fieldDefinition; }
private void AddChangeNotificationHandlerField(PropertyDefinition property, CatelTypeProperty propertyData) { if (propertyData.ChangeCallbackReference == null) { return; } FodyEnvironment.LogDebug($"\t\t\t{property.Name} - adding On{property.Name}Changed invocation"); var declaringType = property.DeclaringType; string fieldName = GetChangeNotificationHandlerFieldName(property); var handlerType = GetEventHandlerAdvancedPropertyChangedEventArgs(property); var advancedPropertyChangedEventArgsType = property.Module.FindType("Catel.Core", "Catel.Data.AdvancedPropertyChangedEventArgs"); //.field private static class [mscorlib]System.EventHandler`1<class [Catel.Core]Catel.Data.AdvancedPropertyChangedEventArgs> CS$<>9__CachedAnonymousMethodDelegate1 var field = new FieldDefinition(fieldName, FieldAttributes.Private | FieldAttributes.Static, declaringType.Module.Import(handlerType)); declaringType.Fields.Add(field); field.MarkAsCompilerGenerated(_msCoreReferenceFinder); //.method private hidebysig static void <.cctor>b__0(object s, class [Catel.Core]Catel.Data.AdvancedPropertyChangedEventArgs e) cil managed //{ // .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() // .maxstack 8 // L_0000: ldarg.0 // L_0001: castclass Catel.Fody.TestAssembly.ModelBaseTest // L_0006: callvirt instance void Catel.Fody.TestAssembly.ModelBaseTest::OnLastNameChanged() // L_000b: nop // L_000c: ret //} var voidType = _msCoreReferenceFinder.GetCoreTypeReference("Void"); var objectType = _msCoreReferenceFinder.GetCoreTypeReference("Object"); string initializationMethodName = GetChangeNotificationHandlerConstructorName(property); var initializationMethod = new MethodDefinition(initializationMethodName, MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.Static, declaringType.Module.Import(voidType)); initializationMethod.Parameters.Add(new ParameterDefinition("s", ParameterAttributes.None, declaringType.Module.Import(objectType))); initializationMethod.Parameters.Add(new ParameterDefinition("e", ParameterAttributes.None, declaringType.Module.Import(advancedPropertyChangedEventArgsType))); var body = initializationMethod.Body; body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Castclass, declaringType.MakeGenericIfRequired()), Instruction.Create(OpCodes.Callvirt, propertyData.ChangeCallbackReference), Instruction.Create(OpCodes.Nop), Instruction.Create(OpCodes.Ret)); declaringType.Methods.Add(initializationMethod); initializationMethod.MarkAsCompilerGenerated(_msCoreReferenceFinder); }