private static CommonProperty CreateCommonProperty(MonoCecilProperty monoCecilProperty, IEnumerable <ReflectionProperty> reflectionProperties) { var matchedReflectionProperty = reflectionProperties.FirstOrDefault(reflectionProperty => reflectionProperty.Name == monoCecilProperty.Name); if (matchedReflectionProperty == null) { return(null); } var propertyAttributes = JoinAttributes(matchedReflectionProperty.Attributes, monoCecilProperty.Attributes); return(new CommonProperty(propertyAttributes, monoCecilProperty, matchedReflectionProperty)); }
private void GenerateGetMethodBody(MonoCecilProperty property, MonoCecilField backgroundField) { log.Info("Generate get method body..."); var propertyGetMethod = property.GetMethod; if (propertyGetMethod == null) { const string errorMessage = "Patching property must have get method accessor"; log.Error(errorMessage); throw new PropertyPatchingException(errorMessage, property.FullName); } var getMethodBodyInstructions = propertyGetMethod.Body.Instructions; getMethodBodyInstructions.Clear(); getMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Ldarg_0)); getMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Ldfld, backgroundField)); getMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Ret)); propertyGetMethod.RemoveAttribute(typeof(CompilerGeneratedAttribute)); log.Info("Get method body was generated"); }
private void GenerateSetMethodBody(MonoCecilAssembly monoCecilAssembly, CommonType viewModelBase, MonoCecilProperty property, string propertyName, MonoCecilField backgroundField) { log.Info("Generate method reference on Set method in ViewModelBase..."); var propertySetMethod = property.SetMethod; if (propertySetMethod == null) { const string errorMessage = "Patching property must have set method accessor"; log.Error(errorMessage); throw new PropertyPatchingException(errorMessage, property.FullName); } var setMethodFromViewModelBase = monoCecilFactory.CreateGenericInstanceMethod(GetSetMethodFromViewModelBase(viewModelBase.MonoCecilType)); setMethodFromViewModelBase.AddGenericArgument(property.PropertyType); var setMethodInViewModelBaseWithGenericParameter = monoCecilAssembly.MainModule.Import(setMethodFromViewModelBase); log.Info("Generate set method body..."); var setMethodBodyInstructions = propertySetMethod.Body.Instructions; setMethodBodyInstructions.Clear(); setMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Ldarg_0)); setMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Ldstr, propertyName)); setMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Ldarg_0)); setMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Ldflda, backgroundField)); setMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Ldarg_1)); setMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Ldc_I4_0)); setMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Call, setMethodInViewModelBaseWithGenericParameter)); setMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Pop)); setMethodBodyInstructions.Add(monoCecilFactory.CreateInstruction(OpCodes.Ret)); propertySetMethod.RemoveAttribute(typeof(CompilerGeneratedAttribute)); log.Info("Set method body was generated"); }
internal CommonProperty(CommonAttribute[] attributes, MonoCecilProperty monoCecilProperty, ReflectionProperty reflectionProperty) { Attributes = attributes; MonoCecilProperty = monoCecilProperty; ReflectionProperty = reflectionProperty; }