예제 #1
0
        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;
        }