public static CustomAttribute InjectCustomAttribute(this ICustomAttributeProvider provider, CustomAttribute attribute, ReferenceResolver resolver)
        {
            if (attribute == null)
                throw new ArgumentNullException("attribute");
            if (resolver == null)
                throw new ArgumentNullException("resolver");

            TypeReference attributeType = resolver.ReferenceType(attribute.AttributeType);

            // no context required as attributes cannot be generic
            MethodReference constructor = resolver.ReferenceMethod(attribute.Constructor);

            CustomAttribute newAttribute;
            if ((newAttribute = Helper.GetCustomAttribute(provider.CustomAttributes, attribute)) != null)
                return newAttribute;

            newAttribute = new CustomAttribute(constructor);//, attr.GetBlob());
            provider.CustomAttributes.Add(newAttribute);

            MetadataBuilderHelper.CopyCustomAttributeArguments(attribute.ConstructorArguments, newAttribute.ConstructorArguments, resolver);

            MetadataBuilderHelper.CopyCustomAttributeNamedArguments(attribute.Fields, newAttribute.Fields, resolver);

            MetadataBuilderHelper.CopyCustomAttributeNamedArguments(attribute.Properties, newAttribute.Properties, resolver);

            return newAttribute;
        }
Exemplo n.º 2
0
        private static void CopyOverrides(MethodDefinition sourceMethod, MethodDefinition targetMethod, ReferenceResolver resolver)
        {
            if (!sourceMethod.HasOverrides)
                return;

            foreach (var methodOverride in sourceMethod.Overrides)
                targetMethod.Overrides.Add(resolver.ReferenceMethod(methodOverride, targetMethod, targetMethod.DeclaringType));
        }
Exemplo n.º 3
0
        private static void CopyInstructions(MethodDefinition sourceMethod, MethodDefinition targetMethod, ReferenceResolver resolver)
        {
            TypeDefinition targetType = targetMethod.DeclaringType;

            var processor = targetMethod.Body.GetILProcessor();
            var offset = 0;
            foreach (var instruction in sourceMethod.Body.Instructions)
            {
                object operand;

                if (instruction.Operand is FieldReference)
                {
                    operand = resolver.ReferenceField((FieldReference)instruction.Operand, targetMethod, targetType);
                }
                else if (instruction.Operand is MethodReference)
                {
                    operand = resolver.ReferenceMethod((MethodReference)instruction.Operand, targetMethod, targetType);
                }
                else if (instruction.Operand is TypeReference)
                {
                    operand = resolver.ReferenceType((TypeReference)instruction.Operand, targetMethod, targetType);
                }
                else
                {
                    operand = instruction.Operand;
                }

                Instruction newInstruction = Helper.CreateInstruction(instruction.OpCode, instruction.OpCode.OperandType, operand);
                newInstruction.SequencePoint = instruction.SequencePoint;

                newInstruction.Offset = offset;
                offset += newInstruction.GetSize();

                processor.Append(newInstruction);
            }

            FixBranchingTargets(targetMethod.Body);
            CopyExceptionHandlers(sourceMethod, targetMethod, resolver);
        }