private void ModifyField(MemberActionAttribute fieldActionAttr, FieldDefinition yourField, FieldDefinition targetField) { Log_modifying_member("field", yourField); (fieldActionAttr != null).AssertTrue(); ModificationScope scope = fieldActionAttr.Scope; if ((scope & ModificationScope.Accessibility) != 0) { targetField.SetAccessibility(yourField.GetAccessbility()); } var attrFilter = AttrFilter(scope); CopyCustomAttributes(targetField, yourField, attrFilter); if ((scope & ModificationScope.Body) != 0) { targetField.InitialValue = yourField.InitialValue; //dunno what this is used for targetField.Constant = yourField.Constant; targetField.HasConstant = yourField.HasConstant; } var toggleAttributesAttr = yourField.GetCustomAttribute <ToggleFieldAttributes>(); var toggleValue = toggleAttributesAttr?.Attributes ?? 0; targetField.Attributes ^= (FieldAttributes)toggleValue; }
private void ModifyEvent(MemberActionAttribute eventActionAttr, EventDefinition yourEvent, EventDefinition targetEvent) { Log_modifying_member("property", yourEvent); ModificationScope scope = eventActionAttr.Scope; var attrFilter = AttrFilter(scope); CopyCustomAttributes(targetEvent, yourEvent, attrFilter); if ((scope & ModificationScope.Body) != 0) { targetEvent.AddMethod = yourEvent.AddMethod != null?FixMethodReference(yourEvent.AddMethod).Resolve() : null; targetEvent.RemoveMethod = yourEvent.RemoveMethod != null ? FixMethodReference(yourEvent.RemoveMethod).Resolve() : null; targetEvent.InvokeMethod = yourEvent.InvokeMethod != null ? FixMethodReference(yourEvent.InvokeMethod).Resolve() : null; targetEvent.OtherMethods.Clear(); if (yourEvent.HasOtherMethods) { //I have absolutely NO idea what this is used for foreach (var otherMethod in yourEvent.OtherMethods) { targetEvent.OtherMethods.Add(FixMethodReference(otherMethod).Resolve()); } } } }
private void ModifyMethod(TypeDefinition targetType, MethodDefinition yourMethod, MemberActionAttribute memberAction, MethodDefinition targetMethod) { Log_modifying_member("method", yourMethod); var insertAttribute = yourMethod.GetCustomAttribute <DuplicatesBodyAttribute>(); var bodySource = insertAttribute == null ? yourMethod : GetBodySource(targetType, yourMethod, insertAttribute); ModificationScope scope = memberAction.Scope; if (scope.HasFlag(AdvancedModificationScope.ExplicitOverrides)) { targetMethod.Overrides.Clear(); foreach (var explicitOverride in yourMethod.Overrides) { targetMethod.Overrides.Add(FixMethodReference(explicitOverride)); } } var attrFilter = AttrFilter(scope); CopyCustomAttributes(targetMethod, yourMethod, attrFilter); CopyCustomAttributes(targetMethod.MethodReturnType, yourMethod.MethodReturnType, attrFilter); for (int i = 0; i < yourMethod.Parameters.Count; i++) { CopyCustomAttributes(targetMethod.Parameters[i], yourMethod.Parameters[i], attrFilter); } for (int i = 0; i < yourMethod.GenericParameters.Count; i++) { CopyCustomAttributes(targetMethod.GenericParameters[i], yourMethod.GenericParameters[i], attrFilter); } if ((scope & ModificationScope.Accessibility) != 0) { targetMethod.SetAccessibility(yourMethod.GetAccessbility()); } if (scope.HasFlag(ModificationScope.Body)) { if (yourMethod.Body != null) { TransferMethodBody(targetMethod, bodySource); } else { //this happens in abstract methods and some others. targetMethod.Body = null; } } if (EmbedHistory) { targetMethod.AddPatchedByMemberAttribute(yourMethod); } var toggleAttributesAttr = yourMethod.GetCustomAttribute <ToggleMethodAttributes>(); var toggleValue = toggleAttributesAttr?.Attributes ?? 0; targetMethod.Attributes ^= (MethodAttributes)toggleValue; }
private static string GetPatchedMemberName(IMemberDefinition yourMemberDef, MemberActionAttribute actionAttribute = null) { actionAttribute = actionAttribute ?? yourMemberDef.GetCustomAttribute <MemberActionAttribute>(); var asModifiesAttr = actionAttribute as ModifiesMemberAttribute; var newMemberName = actionAttribute as NewMemberAttribute; var asAliasedMember = actionAttribute as MemberAliasAttribute; var memberName = asModifiesAttr?.MemberName ?? asAliasedMember?.AliasedMemberName ?? newMemberName?.NewMemberName ?? yourMemberDef.Name; return(memberName); }
private void ModifyProperty(MemberActionAttribute propActionAttr, PropertyDefinition yourProp, PropertyDefinition targetProp) { Log_modifying_member("property", yourProp); ModificationScope scope = propActionAttr.Scope; var attrFilter = AttrFilter(scope); CopyCustomAttributes(targetProp, yourProp, attrFilter); for (int i = 0; i < yourProp.Parameters.Count; i++) { CopyCustomAttributes(targetProp.Parameters[i], yourProp.Parameters[i], attrFilter); } if ((scope & ModificationScope.Body) != 0) { targetProp.GetMethod = yourProp.GetMethod != null ? FixMethodReference(yourProp.GetMethod).Resolve() : null; targetProp.SetMethod = yourProp.SetMethod != null ? FixMethodReference(yourProp.SetMethod).Resolve() : null; targetProp.OtherMethods.Clear(); if (yourProp.HasOtherMethods) { //I have absolutely NO idea what this is used for foreach (var otherMethod in yourProp.OtherMethods) { targetProp.OtherMethods.Add(FixMethodReference(otherMethod).Resolve()); } } } }
private void ModifyMethod(TypeDefinition targetType, MethodDefinition yourMethod, MemberActionAttribute memberAction, MethodDefinition targetMethod) { Log_modifying_member("method", yourMethod); var insertAttribute = yourMethod.GetCustomAttribute<DuplicatesBodyAttribute>(); var bodySource = insertAttribute == null ? yourMethod : GetBodySource(targetType, yourMethod, insertAttribute); ModificationScope scope = memberAction.Scope; if (scope.HasFlag(AdvancedModificationScope.ExplicitOverrides)) { targetMethod.Overrides.Clear(); foreach (var explicitOverride in yourMethod.Overrides) { targetMethod.Overrides.Add(FixMethodReference(explicitOverride)); } } var attrFilter = AttrFilter(scope); CopyCustomAttributes(targetMethod, yourMethod, attrFilter); CopyCustomAttributes(targetMethod.MethodReturnType, yourMethod.MethodReturnType, attrFilter); for (int i = 0; i < yourMethod.Parameters.Count; i++) { CopyCustomAttributes(targetMethod.Parameters[i], yourMethod.Parameters[i], attrFilter); } for (int i = 0; i < yourMethod.GenericParameters.Count; i++) { CopyCustomAttributes(targetMethod.GenericParameters[i], yourMethod.GenericParameters[i], attrFilter); } if ((scope & ModificationScope.Accessibility) != 0) { targetMethod.SetAccessibility(yourMethod.GetAccessbility()); } if (scope.HasFlag(ModificationScope.Body)) { if (yourMethod.Body != null) { TransferMethodBody(targetMethod, bodySource); } else { //this happens in abstract methods and some others. targetMethod.Body = null; } } if (EmbedHistory) { targetMethod.AddPatchedByMemberAttribute(yourMethod); } var toggleAttributesAttr = yourMethod.GetCustomAttribute<ToggleMethodAttributes>(); var toggleValue = toggleAttributesAttr?.Attributes ?? 0; targetMethod.Attributes ^= (MethodAttributes) toggleValue; }
private void ModifyField(MemberActionAttribute fieldActionAttr, FieldDefinition yourField, FieldDefinition targetField) { Log_modifying_member("field", yourField); (fieldActionAttr != null).AssertTrue(); ModificationScope scope = fieldActionAttr.Scope; if ((scope & ModificationScope.Accessibility) != 0) { targetField.SetAccessibility(yourField.GetAccessbility()); } var attrFilter = AttrFilter(scope); CopyCustomAttributes(targetField, yourField, attrFilter); if ((scope & ModificationScope.Body) != 0) { targetField.InitialValue = yourField.InitialValue; //dunno what this is used for targetField.Constant = yourField.Constant; targetField.HasConstant = yourField.HasConstant; } var toggleAttributesAttr = yourField.GetCustomAttribute<ToggleFieldAttributes>(); var toggleValue = toggleAttributesAttr?.Attributes ?? 0; targetField.Attributes ^= (FieldAttributes) toggleValue; }
private void ModifyEvent(MemberActionAttribute eventActionAttr, EventDefinition yourEvent, EventDefinition targetEvent) { Log_modifying_member("property", yourEvent); ModificationScope scope = eventActionAttr.Scope; var attrFilter = AttrFilter(scope); CopyCustomAttributes(targetEvent, yourEvent, attrFilter); if ((scope & ModificationScope.Body) != 0) { targetEvent.AddMethod = yourEvent.AddMethod != null ? FixMethodReference(yourEvent.AddMethod).Resolve() : null; targetEvent.RemoveMethod = yourEvent.RemoveMethod != null ? FixMethodReference(yourEvent.RemoveMethod).Resolve() : null; targetEvent.InvokeMethod = yourEvent.InvokeMethod != null ? FixMethodReference(yourEvent.InvokeMethod).Resolve() : null; targetEvent.OtherMethods.Clear(); if (yourEvent.HasOtherMethods) { //I have absolutely NO idea what this is used for foreach (var otherMethod in yourEvent.OtherMethods) { targetEvent.OtherMethods.Add(FixMethodReference(otherMethod).Resolve()); } } } }