Exemple #1
0
        private static CommonField CreateCommonField(MonoCecilField monoCecilField, IEnumerable <ReflectionField> reflectionFields)
        {
            var matchedReflectionField = reflectionFields.FirstOrDefault(propertyInfo => propertyInfo.Name == monoCecilField.Name);

            if (matchedReflectionField == null)
            {
                return(null);
            }

            var fieldAttributes = JoinAttributes(matchedReflectionField.Attributes, monoCecilField.Attributes);

            return(new CommonField(fieldAttributes, monoCecilField, matchedReflectionField));
        }
        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");
        }
 public virtual MonoCecilInstruction CreateInstruction(OpCode opCode, MonoCecilField monoCecilField)
 {
     return(new MonoCecilInstruction(Instruction.Create(opCode, monoCecilField.Instance)));
 }
        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");
        }
Exemple #5
0
 internal CommonField(CommonAttribute[] attributes, MonoCecilField monoCecilField, ReflectionField reflectionField)
 {
     Attributes      = attributes;
     MonoCecilField  = monoCecilField;
     ReflectionField = reflectionField;
 }