private void GenerateSetMethodBody(ICommonAssembly assembly, ICommonType frameworkElementType, PropertyDefinition property, FieldReference field) { log.Info("Generate set method body..."); if (property.SetMethod == null) { log.Debug($"Create set accessor method for property '{property.Name}'"); var setMethod = new MethodDefinition($"set_{property.Name}", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, assembly.MonoCecil.MainModule.TypeSystem.Void); setMethod.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, property.PropertyType)); property.SetMethod = setMethod; frameworkElementType.MonoCecil.Methods.Add(setMethod); } property.GetMethod.Body.Variables.Clear(); property.SetMethod.Body.Instructions.Clear(); property.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); property.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldsfld, field)); property.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1)); if (property.PropertyType.IsValueType) { property.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Box, property.PropertyType)); } property.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Call, setValueMethod.GetValue(assembly))); property.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); property.SetMethod.RemoveAttributes <CompilerGeneratedAttribute>(); log.Info("Set method body was generated"); }
public override PatchResult Patch(ICommonAssembly assembly, ICommonType viewModelBaseType, ICommonType viewModelType, ViewModelPatchingType patchingType) { log.Info("Patching command groups..."); var commandGroups = commandGrouperService.GetGroups(assembly, viewModelType, patchingType); if (!commandGroups.Any()) { log.Info("Not found command groups"); return(PatchResult.Continue); } log.Debug("Command groups found:", commandGroups.Select(group => $"Execute method: '{group.CommandExecuteMethod.Name}', " + $"can execute method: '{group.CommandCanExecuteMethod?.Name}', " + $"property: '{group.CommandProperty?.Name}', " + $"field: '{group.CommandField?.Name}'")); foreach (var group in commandGroups) { log.Info("Patch group: " + $"Execute method: '{group.CommandExecuteMethod.Name}', " + $"can execute method: '{group.CommandCanExecuteMethod?.Name}', " + $"property: '{group.CommandProperty?.Name}', " + $"field: '{group.CommandField?.Name}'..."); PatchGroup(assembly, viewModelType, group); log.Info("Group was patched"); } log.Info("Command groups was patched"); return(PatchResult.Continue); }
public PropertyGroup[] GetGroups(ICommonAssembly assembly, ICommonType viewModelType, ViewModelPatchingType patchingType) { var commandType = assembly.GetCommonType(KnownTypeNames.ICommand, true); CheckViewModel(commandType, viewModelType); var patchingProperties = GetPatchingProperties(viewModelType, commandType, patchingType); CheckPatchingPropertyNames(patchingProperties); var patchingPropertyGroups = GetPatchingPropertyGroups(patchingProperties, viewModelType, commandType); CheckPatchingPropertyGroups(patchingPropertyGroups); return(patchingPropertyGroups .Select(group => { var patchingPropertyAttribute = group.Property.GetCastedAttribute <PatchingPropertyAttribute>(); return new PropertyGroup( group.Fields.SingleOrDefault(), group.Property, GetCalledMethod(viewModelType, patchingPropertyAttribute?.CalledMethodNameBeforeGetProperty), GetCalledMethod(viewModelType, patchingPropertyAttribute?.CalledMethodNameBeforeSetProperty), GetCalledMethod(viewModelType, patchingPropertyAttribute?.CalledMethodNameAfterSuccessSetProperty), GetCalledMethod(viewModelType, patchingPropertyAttribute?.CalledMethodNameAfterSetProperty)); }) .ToArray()); }
private void GenerateGetMethodBody(ICommonAssembly assembly, ICommonType frameworkElementType, PropertyDefinition property, FieldReference field) { log.Info("Generate get method body..."); if (property.GetMethod == null) { log.Debug($"Create get accessor method for property '{property.Name}'"); var getMethod = new MethodDefinition($"get_{property.Name}", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, property.PropertyType); property.GetMethod = getMethod; frameworkElementType.MonoCecil.Methods.Add(getMethod); } property.GetMethod.Body.Variables.Clear(); property.GetMethod.Body.Instructions.Clear(); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldsfld, field)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Call, getValueMethod.GetValue(assembly))); property.GetMethod.Body.Instructions.Add(Instruction.Create(property.PropertyType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, property.PropertyType)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); property.GetMethod.RemoveAttributes <CompilerGeneratedAttribute>(); log.Info("Get method body was generated"); }
private void CreateEmptyInternalConstructor(ICommonAssembly assembly, ICommonType type) { log.Info("Create constructor without parameters..."); var emptyConstructor = type.GetConstructor(); if (emptyConstructor != null) { emptyConstructor.MonoCecil.IsAssembly = true; log.Info("Constructor without parameters already created"); } else { const MethodAttributes emptyConstructorMethodAttributes = MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; var objectEmptyConstructorMethod = assembly.MonoCecil.MainModule.ImportReference(typeof(object).GetConstructor(Type.EmptyTypes)); var emptyConstructorMethod = new MethodDefinition(".ctor", emptyConstructorMethodAttributes, assembly.MonoCecil.MainModule.TypeSystem.Void); emptyConstructorMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); emptyConstructorMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Call, objectEmptyConstructorMethod)); emptyConstructorMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); type.MonoCecil.Methods.Add(emptyConstructorMethod); log.Info("Constructor without parameters was created"); } }
private static void RemoveAttributesFromViewModels(ICommonAssembly assembly) { var viewModelBaseType = assembly.GetCommonType(KnownTypeNames.ViewModelBase); var viewModelTypes = assembly.GetInheritanceCommonTypesFromThisAssembly(viewModelBaseType).ToArray(); foreach (var viewModelType in viewModelTypes.Select(type => type.Load())) { viewModelType.MonoCecil.RemoveAttributes <PatchingViewModelAttribute>(); viewModelType.MonoCecil.RemoveAttributes <NotPatchingViewModelAttribute>(); foreach (var property in viewModelType.Fields) { property.MonoCecil.RemoveAttributes <ConnectFieldToPropertyAttribute>(); } foreach (var property in viewModelType.Properties) { property.MonoCecil.RemoveAttributes <NotUseSearchByNameAttribute>(); property.MonoCecil.RemoveAttributes <PatchingPropertyAttribute>(); property.MonoCecil.RemoveAttributes <NotPatchingPropertyAttribute>(); property.MonoCecil.RemoveAttributes <ConnectPropertyToFieldAttribute>(); property.MonoCecil.RemoveAttributes <ConnectPropertyToMethodAttribute>(); } foreach (var property in viewModelType.Methods) { property.MonoCecil.RemoveAttributes <NotUseSearchByNameAttribute>(); property.MonoCecil.RemoveAttributes <PatchingCommandAttribute>(); property.MonoCecil.RemoveAttributes <NotPatchingCommandAttribute>(); property.MonoCecil.RemoveAttributes <ConnectMethodToMethodAttribute>(); property.MonoCecil.RemoveAttributes <ConnectMethodToPropertyAttribute>(); } } }
public override PatchResult Patch(ICommonAssembly assembly) { RewriteMainAssemblyPublicKey(assembly); RewriteSelectedAssemblyReferencesPublicKey(assembly); RewriteOrCreateInternalVisibleToAttributes(assembly); return(PatchResult.Continue); }
public void Save(ICommonAssembly commonAssembly, string assemblyPath, string signaturePath = null) { var strongNameKeyPair = signaturePath.IsNullOrEmpty() ? null : new StrongNameKeyPair(File.ReadAllBytes(signaturePath)); commonAssembly.MonoCecil.Write(assemblyPath, new WriterParameters { WriteSymbols = commonAssembly.HaveSymbolStore, StrongNameKeyPair = strongNameKeyPair }); }
public override PatchResult Patch(ICommonAssembly assembly) { log.Info("Remove framework element attributes..."); RemoveAttributesFromFrameworkElements(assembly); log.Info("Framework element attributes was removed"); return(PatchResult.Continue); }
private void PatchGroup(ICommonAssembly assembly, ICommonType frameworkElementType, DependencyGroup group) { RemoveDefaultField(frameworkElementType, group.Property.Name); var field = group.Field?.MonoCecil ?? CreateField(assembly, frameworkElementType, group.Property); GenerateGetMethodBody(assembly, frameworkElementType, group.Property.MonoCecil, field); GenerateSetMethodBody(assembly, frameworkElementType, group.Property.MonoCecil, field); }
private void RewriteMainAssemblyPublicKey(ICommonAssembly assembly) { log.Info("Rewrite assembly public key..."); assembly.MonoCecil.Name.PublicKey = applicationPatcherSelfConfiguration.MonoCecilNewPublicKey; assembly.MonoCecil.Name.PublicKeyToken = applicationPatcherSelfConfiguration.MonoCecilNewPublicKeyToken; log.Info("Assembly public key was rewrited"); }
public override PatchResult Patch(ICommonAssembly assembly) { log.Info("Remove assembly attributes..."); RemoveAttributesFromAssembly(assembly); log.Info("Assembly attributes was removed"); return(PatchResult.Continue); }
private void PatchGroup(ICommonAssembly assembly, IHasMethods viewModelBaseType, ICommonType viewModelType, PropertyGroup group) { RemoveDefaultField(viewModelType, group.Property.Name); var field = group.Field?.MonoCecil ?? CreateField(viewModelType, group.Property); GenerateGetMethodBody(viewModelType, group, field); GenerateSetMethodBody(assembly, viewModelBaseType, viewModelType, group, field); }
public override PatchResult Patch(ICommonAssembly assembly) { log.Info("Remove view model attributes..."); RemoveAttributesFromViewModels(assembly); log.Info("View model attributes was removed"); return(PatchResult.Continue); }
private void GenerateSetMethodBody(ICommonAssembly assembly, IHasMethods viewModelBaseType, ICommonType viewModelType, PropertyGroup group, FieldReference field) { log.Info("Generate set method body..."); var property = group.Property; if (property.SetMethod == null) { log.Debug($"Create set accessor method for property '{property.Name}'"); var setMethod = new MethodDefinition($"set_{property.Name}", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, assembly.MonoCecil.MainModule.TypeSystem.Void); setMethod.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, property.MonoCecil.PropertyType)); property.MonoCecil.SetMethod = setMethod; viewModelType.MonoCecil.Methods.Add(setMethod); } var setMethodFromViewModelBaseWithGeneric = new GenericInstanceMethod(viewModelBaseType.GetMethod("Set", new[] { typeof(string).FullName, "T&", "T", typeof(bool).FullName }, true).MonoCecil); setMethodFromViewModelBaseWithGeneric.GenericArguments.Add(property.MonoCecil.PropertyType); var importedSetMethodFromViewModelBaseWithGeneric = assembly.MonoCecil.MainModule.ImportReference(setMethodFromViewModelBaseWithGeneric); var callMethodAfterSetPropertyInstructions = CreateCallMethodInstructions(group.CalledMethodAfterSetProperty).ToArray(); property.MonoCecil.SetMethod.Body.Variables.Clear(); property.MonoCecil.SetMethod.Body.Instructions.Clear(); CreateCallMethodInstructions(group.CalledMethodBeforeSetProperty).ForEach(property.MonoCecil.SetMethod.Body.Instructions.Add); property.MonoCecil.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); property.MonoCecil.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldstr, property.Name)); property.MonoCecil.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); property.MonoCecil.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldflda, field)); property.MonoCecil.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1)); property.MonoCecil.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_0)); property.MonoCecil.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Call, importedSetMethodFromViewModelBaseWithGeneric)); var returnInstruction = Instruction.Create(OpCodes.Ret); if (group.CalledMethodAfterSuccessSetProperty != null) { var firstInstructionAfterJump = callMethodAfterSetPropertyInstructions.FirstOrDefault() ?? returnInstruction; property.MonoCecil.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Brfalse_S, firstInstructionAfterJump)); CreateCallMethodInstructions(group.CalledMethodAfterSuccessSetProperty).ForEach(property.MonoCecil.SetMethod.Body.Instructions.Add); } else { property.MonoCecil.SetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Pop)); } callMethodAfterSetPropertyInstructions.ForEach(property.MonoCecil.SetMethod.Body.Instructions.Add); property.MonoCecil.SetMethod.Body.Instructions.Add(returnInstruction); property.MonoCecil.SetMethod.RemoveAttributes <CompilerGeneratedAttribute>(); log.Info("Set method body was generated"); }
private void PatchGroup(ICommonAssembly assembly, ICommonType viewModelType, CommandGroup group) { var property = group.CommandProperty?.MonoCecil ?? CreateProperty(assembly, viewModelType, group.CommandExecuteMethod.Name); RemoveDefaultField(viewModelType, property.Name); var field = group.CommandField?.MonoCecil ?? CreateField(viewModelType, property); RemoveSetMethod(viewModelType, property); GenerateGetMethodBody(assembly, viewModelType, group.CommandExecuteMethod, group.CommandCanExecuteMethod, property, field); }
private void GenerateGetMethodBody(ICommonAssembly assembly, ICommonType viewModelType, ICommonMethod executeMethod, ICommonMethod canExecuteMethod, PropertyDefinition property, FieldReference field) { log.Info("Generate get method body..."); if (property.GetMethod == null) { log.Debug($"Create get accessor method for property '{property.Name}'"); var getMethod = new MethodDefinition($"get_{property.Name}", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, property.PropertyType); property.GetMethod = getMethod; viewModelType.MonoCecil.Methods.Add(getMethod); } var returnInstruction = Instruction.Create(OpCodes.Ret); property.GetMethod.Body.Variables.Clear(); property.GetMethod.Body.InitLocals = true; property.GetMethod.Body.Variables.Add(new VariableDefinition(assembly.MonoCecil.MainModule.ImportReference(commandType.GetValue(assembly).MonoCecil))); property.GetMethod.Body.Instructions.Clear(); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldfld, field)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Dup)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Brtrue_S, returnInstruction)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Pop)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldftn, executeMethod.MonoCecil)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Newobj, actionConstructor.GetValue(assembly))); if (canExecuteMethod == null) { property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_0)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Newobj, relayCommandConstructor.GetValue(assembly))); } else { property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldftn, canExecuteMethod.MonoCecil)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Newobj, funcBoolConstructor.GetValue(assembly))); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_0)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Newobj, relayCommandConstructorWithCanExecuteMethod.GetValue(assembly))); } property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Dup)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Stloc_0)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Stfld, field)); property.GetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldloc_0)); property.GetMethod.Body.Instructions.Add(returnInstruction); property.GetMethod.RemoveAttributes <CompilerGeneratedAttribute>(); log.Info("Get method body was generated"); }
private PropertyDefinition CreateProperty(ICommonAssembly assembly, ICommonType viewModelType, string executeMethodName) { var propertyName = nameRulesService.ConvertName(executeMethodName, UseNameRulesFor.CommandExecuteMethod, UseNameRulesFor.CommandProperty); log.Debug($"Create property with name '{propertyName}'"); var property = new PropertyDefinition(propertyName, PropertyAttributes.None, assembly.MonoCecil.MainModule.ImportReference(commandType.GetValue(assembly).MonoCecil)); viewModelType.MonoCecil.Properties.Add(property); return(property); }
private void RewriteOrCreateInternalVisibleToAttributes(ICommonAssembly assembly) { log.Info($"Create InternalsVisibleToAttribute for '{MoqAssemblyName}'..."); var internalsVisibleToAttributes = assembly.GetAttributes <InternalsVisibleToAttribute>() .Select(commonAttribute => { var constructorArgument = commonAttribute.MonoCecil.ConstructorArguments.FirstOrDefault(); var attributeParams = ((string)constructorArgument.Value).Split(',').Select(x => x.Trim()).ToList(); var assemblyName = attributeParams.FirstOrDefault(); return(new { AssemblyName = assemblyName, AttributeParams = attributeParams, ConstructorArgument = constructorArgument, commonAttribute.MonoCecil }); }) .ToArray(); if (internalsVisibleToAttributes.Any(attribute => attribute.AssemblyName == MoqAssemblyName)) { log.Info($"InternalsVisibleToAttribute for '{MoqAssemblyName}' already created"); } else { var internalsVisibleToAttribute = new CustomAttribute(assembly.MonoCecil.MainModule.ImportReference(typeof(InternalsVisibleToAttribute).GetConstructor(new[] { typeof(string) }))); internalsVisibleToAttribute.ConstructorArguments.Add(new CustomAttributeArgument(assembly.MonoCecil.MainModule.TypeSystem.String, $"{MoqAssemblyName}, PublicKey={MoqAssemblyPublicKey}")); assembly.MonoCecil.CustomAttributes.Add(internalsVisibleToAttribute); log.Info($"InternalsVisibleToAttribute for '{MoqAssemblyName}' was created"); } log.Info("Rewrite public keys from selected InternalsVisibleToAttributes..."); var selectedInternalsVisibleToAttributes = internalsVisibleToAttributes .Where(attribute => applicationPatcherSelfConfiguration.MonoCecilSelectedInternalsVisibleToAttributeNames.Contains(attribute.AssemblyName)) .ToArray(); if (!selectedInternalsVisibleToAttributes.Any()) { log.Info("Not found selected InternalsVisibleToAttributes"); return; } foreach (var selectedInternalsVisibleToAttribute in selectedInternalsVisibleToAttributes) { var attributeParams = selectedInternalsVisibleToAttribute.AttributeParams .Where(attributeParam => !attributeParam.StartsWith("PublicKey", StringComparison.OrdinalIgnoreCase)) .Concat(new[] { $"PublicKey={applicationPatcherSelfConfiguration.MonoCecilNewPublicKey.ToHexString()}" }) .ToArray(); log.Debug($"Rewrite public key from InternalsVisibleToAttribute with assembly name '{selectedInternalsVisibleToAttribute.AssemblyName}'"); selectedInternalsVisibleToAttribute.MonoCecil.ConstructorArguments.Clear(); selectedInternalsVisibleToAttribute.MonoCecil.ConstructorArguments.Add( new CustomAttributeArgument(selectedInternalsVisibleToAttribute.ConstructorArgument.Type, attributeParams.JoinToString(", "))); } log.Info("Public keys from selected InternalsVisibleToAttributes was rewrited"); }
private PatchResult PatchViewModel(ICommonAssembly assembly, ICommonType viewModelBaseType, ICommonType viewModelType) { log.Info($"Loading type '{viewModelType.FullName}'..."); viewModelType.Load(1); log.Info($"Type '{viewModelType.FullName}' was loaded"); var patchingType = viewModelType.GetReflectionAttribute <PatchingViewModelAttribute>()?.PatchingType ?? applicationPatcherWpfConfiguration.DefaultViewModelPatchingType; log.Info($"View model patching type: '{patchingType}'"); return(PatchHelper.PatchApplication(viewModelPartPatchers, patcher => patcher.Patch(assembly, viewModelBaseType, viewModelType, patchingType), log)); }
public override PatchResult Patch(ICommonAssembly assembly) { log.Info("Check assembly patched attribute..."); if (assembly.ContainsReflectionAttribute <AssemblyPatchedAttribute>()) { log.Info("Assembly patched attribute found"); return(PatchResult.Cancel); } log.Info("Assembly patched attribute not found"); return(PatchResult.Continue); }
private PatchResult PatchFrameworkElement(ICommonAssembly assembly, ICommonType frameworkElementType) { log.Info($"Loading type '{frameworkElementType.FullName}'..."); frameworkElementType.Load(1); log.Info($"Type '{frameworkElementType.FullName}' was loaded"); var patchingType = frameworkElementType.GetReflectionAttribute <PatchingFrameworkElementAttribute>()?.PatchingType ?? applicationPatcherWpfConfiguration.DefaultFrameworkElementPatchingType; log.Info($"Framework element patching type: '{patchingType}'"); return(PatchHelper.PatchApplication(frameworkElementPartPatchers, patcher => patcher.Patch(assembly, frameworkElementType, patchingType), log)); }
private FieldDefinition CreateField(ICommonAssembly assembly, ICommonType frameworkElementType, ICommonProperty property) { var fieldName = nameRulesService.ConvertName(property.Name, UseNameRulesFor.DependencyProperty, UseNameRulesFor.DependencyField); log.Debug($"Create field with name '{fieldName}'"); var field = new FieldDefinition(fieldName, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.InitOnly, assembly.MonoCecil.MainModule.ImportReference(dependencyPropertyType.GetValue(assembly).MonoCecil)); frameworkElementType.MonoCecil.Fields.Add(field); InitializeInStaticConstructor(assembly, frameworkElementType, property, field); return(field); }
public override PatchResult Patch(ICommonAssembly assembly) { log.Info("Patching view model types..."); var selectingType = assembly.GetReflectionAttribute <SelectingViewModelAttribute>()?.SelectingType ?? applicationPatcherWpfConfiguration.DefaultViewModelSelectingType; log.Info($"View model selecting type: '{selectingType}'"); var viewModelBaseType = assembly.GetCommonType(KnownTypeNames.ViewModelBase, true).Load(); CheckAssembly(assembly, viewModelBaseType); var viewModelTypes = assembly.GetInheritanceCommonTypesFromThisAssembly(viewModelBaseType).ToArray(); if (!viewModelTypes.Any()) { log.Info("Not found view model types"); return(PatchResult.Continue); } log.Debug("View model types found:", viewModelTypes.Select(viewModel => viewModel.FullName).OrderBy(fullName => fullName)); var patchingViewModelTypes = viewModelTypes .Where(viewModelType => viewModelType.NotContainsReflectionAttribute <NotPatchingViewModelAttribute>() && (selectingType == ViewModelSelectingType.All || viewModelType.ContainsReflectionAttribute <PatchingViewModelAttribute>())) .ToArray(); if (!patchingViewModelTypes.Any()) { log.Info("Not found patching view model types"); return(PatchResult.Continue); } log.Debug("Patching view model types:", patchingViewModelTypes.Select(viewModel => viewModel.FullName).OrderBy(fullName => fullName)); foreach (var viewModelType in patchingViewModelTypes) { log.Info($"Patching type '{viewModelType.FullName}'..."); if (PatchViewModel(assembly, viewModelBaseType, viewModelType) == PatchResult.Cancel) { return(PatchResult.Cancel); } log.Info($"Type '{viewModelType.FullName}' was patched"); } log.Info("View model types was patched"); return(PatchResult.Continue); }
public override PatchResult Patch(ICommonAssembly assembly) { log.Info("Patching framework element types..."); var selectingType = assembly.GetReflectionAttribute <SelectingFrameworkElementAttribute>()?.SelectingType ?? applicationPatcherWpfConfiguration.DefaultFrameworkElementSelectingType; log.Info($"Framework element selecting type: '{selectingType}'"); var frameworkElementBaseType = assembly.GetCommonType(KnownTypeNames.FrameworkElement, true).Load(); CheckAssembly(assembly, frameworkElementBaseType); var frameworkElementTypes = assembly.GetInheritanceCommonTypesFromThisAssembly(frameworkElementBaseType).ToArray(); if (!frameworkElementTypes.Any()) { log.Info("Not found framework element types"); return(PatchResult.Continue); } log.Debug("Framework element types found:", frameworkElementTypes.Select(frameworkElement => frameworkElement.FullName).OrderBy(fullName => fullName)); var patchingFrameworkElementTypes = frameworkElementTypes .Where(frameworkElementType => frameworkElementType.NotContainsReflectionAttribute <NotPatchingFrameworkElementAttribute>() && (selectingType == FrameworkElementSelectingType.All || frameworkElementType.ContainsReflectionAttribute <PatchingFrameworkElementAttribute>())) .ToArray(); if (!patchingFrameworkElementTypes.Any()) { log.Info("Not found patching framework element types"); return(PatchResult.Continue); } log.Debug("Patching framework element types:", patchingFrameworkElementTypes.Select(frameworkElement => frameworkElement.FullName).OrderBy(fullName => fullName)); foreach (var frameworkElementType in patchingFrameworkElementTypes) { log.Info($"Patching type '{frameworkElementType.FullName}'..."); if (PatchFrameworkElement(assembly, frameworkElementType) == PatchResult.Cancel) { return(PatchResult.Cancel); } log.Info($"Type '{frameworkElementType.FullName}' was patched"); } log.Info("Framework element types was patched"); return(PatchResult.Continue); }
public CommandGroup[] GetGroups(ICommonAssembly assembly, ICommonType viewModelType, ViewModelPatchingType patchingType) { var commandType = assembly.GetCommonType(KnownTypeNames.ICommand, true); CheckViewModel(commandType, viewModelType); var patchingMethods = GetPatchingMethods(viewModelType, patchingType); CheckPatchingMethodNames(patchingMethods); var patchingCommandGroups = GetPatchingCommandGroups(patchingMethods, viewModelType, commandType); CheckPatchingCommandGroups(patchingCommandGroups); return(patchingCommandGroups.Select(group => new CommandGroup(group.Fields.SingleOrDefault(), group.Properties.SingleOrDefault(), group.ExecuteMethod, group.CanExecuteMethods.SingleOrDefault())).ToArray()); }
public DependencyGroup[] GetGroups(ICommonAssembly assembly, ICommonType frameworkElementType, FrameworkElementPatchingType patchingType) { var dependencyPropertyType = assembly.GetCommonType(KnownTypeNames.DependencyProperty, true); CheckFrameworkElement(frameworkElementType, dependencyPropertyType); var patchingProperties = GetPatchingProperties(frameworkElementType, patchingType); CheckPatchingPropertyNames(patchingProperties); var patchingDependencyGroups = GetPatchingDependencyGroups(patchingProperties, frameworkElementType); CheckPatchingPropertyGroups(patchingDependencyGroups, dependencyPropertyType); return(patchingDependencyGroups.Select(group => new DependencyGroup(group.Fields.SingleOrDefault(), group.Property)).ToArray()); }
public override PatchResult Patch(ICommonAssembly assembly) { log.Info("Add assembly patched attribute..."); if (!assembly.NotContainsReflectionAttribute <AssemblyPatchedAttribute>()) { log.Info("Assembly patched attribute already exists"); return(PatchResult.Continue); } var assemblyPatchedAttributeConstructor = assembly.GetCommonType(typeof(AssemblyPatchedAttribute), true).Load().GetConstructor(true); assembly.MonoCecil.CustomAttributes.Add(new CustomAttribute(assembly.MonoCecil.MainModule.ImportReference(assemblyPatchedAttributeConstructor.MonoCecil))); log.Info("Assembly patched attribute was added"); return(PatchResult.Continue); }
public override PatchResult Patch(ICommonAssembly assembly) { const string constsTypeFullName = "Consts"; log.Info($"Patching '{constsTypeFullName}' type..."); var constsType = assembly.GetCommonTypeFromThisAssembly(constsTypeFullName); if (constsType == null) { log.Info($"Not found '{constsTypeFullName}' type"); return(PatchResult.Continue); } constsType.Load().GetField("PublicKey").MonoCecil.Constant = applicationPatcherSelfConfiguration.MonoCecilNewPublicKey.ToHexString(); log.Info($"'{constsTypeFullName}' type was patched"); return(PatchResult.Continue); }
private void InitializeInStaticConstructor(ICommonAssembly assembly, ICommonType frameworkElementType, ICommonProperty property, FieldReference field) { var staticConstructor = frameworkElementType.MonoCecil.GetStaticConstructor(); if (staticConstructor == null) { staticConstructor = new MethodDefinition(".cctor", MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.Static, assembly.MonoCecil.MainModule.TypeSystem.Void); staticConstructor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); frameworkElementType.MonoCecil.Methods.Add(staticConstructor); } staticConstructor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldstr, property.Name)); staticConstructor.Body.Instructions.Insert(1, Instruction.Create(OpCodes.Ldtoken, property.MonoCecil.PropertyType)); staticConstructor.Body.Instructions.Insert(2, Instruction.Create(OpCodes.Call, getTypeFromHandleMethod.GetValue(assembly))); staticConstructor.Body.Instructions.Insert(3, Instruction.Create(OpCodes.Ldtoken, frameworkElementType.MonoCecil)); staticConstructor.Body.Instructions.Insert(4, Instruction.Create(OpCodes.Call, getTypeFromHandleMethod.GetValue(assembly))); staticConstructor.Body.Instructions.Insert(5, Instruction.Create(OpCodes.Call, registerMethod.GetValue(assembly))); staticConstructor.Body.Instructions.Insert(6, Instruction.Create(OpCodes.Stsfld, field)); }