コード例 #1
0
        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;
        }
コード例 #2
0
        public static FieldDefinition InjectField(this TypeDefinition targetType, FieldDefinition sourceField, ReferenceResolver resolver)
        {
            if (sourceField == null)
                throw new ArgumentNullException("sourceField");
            if (resolver == null)
                throw new ArgumentNullException("resolver");

            FieldDefinition newField = null;
            if (Helper.TryGetField(targetType.Fields, sourceField, ref newField))
                return newField;

            TypeReference fieldType = resolver.ReferenceType(sourceField.FieldType, targetType);
            newField = new FieldDefinition(sourceField.Name, sourceField.Attributes, fieldType)
            {
                InitialValue = sourceField.InitialValue,
                DeclaringType = targetType,
            };

            targetType.Fields.Add(newField);

            MetadataBuilderHelper.CopyCustomAttributes(sourceField, newField, resolver);

            if (newField.HasDefault)
                newField.Constant = sourceField.Constant;

            return newField;
        }
コード例 #3
0
 public static void CopyCustomAttributeArguments(Collection<Mono.Cecil.CustomAttributeArgument> source, Collection<Mono.Cecil.CustomAttributeArgument> target, ReferenceResolver resolver)
 {
     foreach (var argument in source)
     {
         var argumentType = resolver.ReferenceType(argument.Type);
         target.Add(new CustomAttributeArgument(argumentType, argument.Value));
     }
 }
コード例 #4
0
        public static void InjectSecurityAttribute(this SecurityDeclaration target, SecurityAttribute attribute, ReferenceResolver resolver)
        {
            var attributeType = resolver.ReferenceType(attribute.AttributeType);
            var newAttribute = new SecurityAttribute(attributeType);

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

            target.SecurityAttributes.Add(newAttribute);
        }
コード例 #5
0
        public static void InjectGenericParameter(this IGenericParameterProvider owner, GenericParameter parameter, ReferenceResolver resolver)
        {
            GenericParameter newParameter = new GenericParameter(parameter.Name, owner)
            {
                Attributes = parameter.Attributes
            };

            owner.GenericParameters.Add(newParameter);

            MetadataBuilderHelper.CopyCustomAttributes(newParameter, parameter, resolver);

            foreach (var constraint in parameter.Constraints)
            {
                if (owner is MethodReference)
                    newParameter.Constraints.Add(resolver.ReferenceType(constraint, owner, ((MethodReference)owner).DeclaringType));
                else
                    newParameter.Constraints.Add(resolver.ReferenceType(constraint, owner));
            }
        }
コード例 #6
0
        public static TypeDefinition CreateNewType(TypeDefinition sourceType, ReferenceResolver resolver)
        {
            TypeDefinition newType = new TypeDefinition(sourceType.Namespace, sourceType.Name, sourceType.Attributes)
            {
                PackingSize = sourceType.PackingSize,
                ClassSize = sourceType.ClassSize,
            };

            CopyGenericParameters(sourceType, newType, resolver);

            if (sourceType.BaseType != null)
            {
                newType.BaseType = resolver.ReferenceType(sourceType.BaseType, newType);
            }

            CopyCustomAttributes(sourceType, newType, resolver);
            return newType;
        }
コード例 #7
0
        public static EventDefinition InjectEvent(this TypeDefinition targetType, EventDefinition sourceEvent, ReferenceResolver resolver)
        {
            if (sourceEvent == null)
                throw new ArgumentNullException("sourceEvent");
            if (resolver == null)
                throw new ArgumentNullException("resolver");

            var eventType = resolver.ReferenceType(sourceEvent.EventType, targetType);

            EventDefinition newEvent = null;
            if (Helper.TryGetEvent(targetType.Events, sourceEvent, ref newEvent))
                return newEvent;

            newEvent = new EventDefinition(sourceEvent.Name, sourceEvent.Attributes, eventType)
            {
                DeclaringType = targetType,
            };

            targetType.Events.Add(newEvent);

            MetadataBuilderHelper.CopyCustomAttributes(sourceEvent, newEvent, resolver);

            if (sourceEvent.AddMethod != null)
            {
                newEvent.AddMethod = targetType.InjectMethod(sourceEvent.AddMethod, resolver);
            }

            if (sourceEvent.RemoveMethod != null)
            {
                newEvent.RemoveMethod = targetType.InjectMethod(sourceEvent.RemoveMethod, resolver);
            }

            foreach (var otherMethod in sourceEvent.OtherMethods)
            {
                newEvent.OtherMethods.Add(targetType.InjectMethod(otherMethod, resolver));
            }

            return newEvent;
        }
コード例 #8
0
        public static MethodDefinition InjectMethod(this TypeDefinition targetType, MethodDefinition sourceMethod, ReferenceResolver resolver, bool body = true)
        {
            if (sourceMethod == null)
                throw new ArgumentNullException("sourceMethod");
            if (resolver == null)
                throw new ArgumentNullException("resolver");

            // Console.WriteLine("\tCreating method {0}", sourceMethod.FullName);

            MethodDefinition newMethod = null;
            if (Helper.TryGetMethod(targetType.Methods, sourceMethod, ref newMethod))
                return newMethod;

            newMethod = new MethodDefinition(sourceMethod.Name, sourceMethod.Attributes, sourceMethod.ReturnType)
            {
                ExplicitThis = sourceMethod.ExplicitThis,
                ImplAttributes = sourceMethod.ImplAttributes,
                SemanticsAttributes = sourceMethod.SemanticsAttributes,
                DeclaringType = targetType,
                CallingConvention = sourceMethod.CallingConvention,
            };

            targetType.Methods.Add(newMethod);

            MetadataBuilderHelper.CopyGenericParameters(sourceMethod, newMethod, resolver);

            CopyParameters(sourceMethod, newMethod, resolver);

            newMethod.ReturnType = resolver.ReferenceType(sourceMethod.ReturnType, newMethod, targetType);

            MetadataBuilderHelper.CopyCustomAttributes(sourceMethod, newMethod, resolver);

            CopyOverrides(sourceMethod, newMethod, resolver);

            MetadataBuilderHelper.CopySecurityDeclarations(sourceMethod, newMethod, resolver);

            if(body)
                CopyMethodBody(sourceMethod, newMethod, resolver);

            //  Console.WriteLine("\tCreated method {0}", sourceMethod.FullName);
            return newMethod;
        }
コード例 #9
0
        private static void CopyVariables(MethodDefinition sourceMethod, MethodDefinition targetMethod, ReferenceResolver resolver)
        {
            if (!sourceMethod.Body.HasVariables)
                return;

            foreach (var variableDefinition in sourceMethod.Body.Variables)
            {
                var variableType = resolver.ReferenceType(variableDefinition.VariableType, targetMethod, targetMethod.DeclaringType);
                VariableDefinition newVariable = new VariableDefinition(variableDefinition.Name, variableType);
                targetMethod.Body.Variables.Add(newVariable);
            }
        }
コード例 #10
0
        private static void CopyParameters(MethodReference sourceMethod, MethodReference targetMethod, ReferenceResolver resolver)
        {
            if (!sourceMethod.HasParameters)
                return;

            //to have full method signature when determining generic patameter owner
            foreach (var parameterDefinition in sourceMethod.Parameters)
                targetMethod.Parameters.Add(parameterDefinition);

            //fix references
            for (int i = 0; i < targetMethod.Parameters.Count; i++)
            {
                var parameterDefinition = targetMethod.Parameters[i];

                var parameterType = resolver.ReferenceType(parameterDefinition.ParameterType, targetMethod, targetMethod.DeclaringType);
                ParameterDefinition newParameter = new ParameterDefinition(parameterDefinition.Name, parameterDefinition.Attributes, parameterType)
                {
                    HasConstant = parameterDefinition.HasConstant,
                    MarshalInfo = parameterDefinition.MarshalInfo,
                };
                targetMethod.Parameters[i] = newParameter;

                if (parameterDefinition.HasDefault)
                    newParameter.Constant = parameterDefinition.Constant;
                MetadataBuilderHelper.CopyCustomAttributes(parameterDefinition, newParameter, resolver);
            }
        }
コード例 #11
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);
        }
コード例 #12
0
        private static void CopyExceptionHandlers(MethodDefinition sourceMethod, MethodDefinition targetMethod, ReferenceResolver resolver)
        {
            if (!sourceMethod.Body.HasExceptionHandlers)
                return;

            foreach (var handler in sourceMethod.Body.ExceptionHandlers)
            {
                var newHandler = new ExceptionHandler(handler.HandlerType)
                {
                    FilterStart = GetInstruction(targetMethod.Body, handler.FilterStart),
             //       FilterEnd = GetInstruction(targetMethod.Body, handler.FilterEnd),
                    HandlerStart = GetInstruction(targetMethod.Body, handler.HandlerStart),
                    HandlerEnd = GetInstruction(targetMethod.Body, handler.HandlerEnd),
                    TryStart = GetInstruction(targetMethod.Body, handler.TryStart),
                    TryEnd = GetInstruction(targetMethod.Body, handler.TryEnd),
                };

                if (handler.CatchType != null)
                    newHandler.CatchType = resolver.ReferenceType(handler.CatchType, targetMethod, targetMethod.DeclaringType);

                targetMethod.Body.ExceptionHandlers.Add(newHandler);
            }
        }
コード例 #13
0
        public static TypeDefinition InjectTypeMembers(this TypeDefinition type, TypeDefinition sourceType, ReferenceResolver resolver)
        {
            if (sourceType == null)
                throw new ArgumentNullException("sourceType");
            if (resolver == null)
                throw new ArgumentNullException("resolver");

            foreach (var nestedType in sourceType.NestedTypes)
                type.InjectNestedType(nestedType, resolver);

            foreach (var @interface in sourceType.Interfaces)
                type.Interfaces.Add(resolver.ReferenceType(@interface, type));

            foreach (var field in sourceType.Fields)
                type.InjectField(field, resolver);

            foreach (var @event in sourceType.Events)
                type.InjectEvent(@event, resolver);

            foreach (var method in sourceType.Methods)
                type.InjectMethod(method, resolver);

            foreach (var property in sourceType.Properties)
                type.InjectProperty(property, resolver);

            MetadataBuilderHelper.CopySecurityDeclarations(sourceType, type, resolver);

            Console.WriteLine("Created type {0}", type.FullName);
            return type;
        }
コード例 #14
0
        public static PropertyDefinition InjectProperty(this TypeDefinition targetType, PropertyDefinition sourceProperty, ReferenceResolver resolver)
        {
            if (sourceProperty == null)
                throw new ArgumentNullException("sourceProperty");
            if (resolver == null)
                throw new ArgumentNullException("resolver");

            TypeReference propertyType = resolver.ReferenceType(sourceProperty.PropertyType, targetType);

            PropertyDefinition newProperty = null;
            if (Helper.TryGetProperty(targetType.Properties, sourceProperty, ref newProperty))
                return newProperty;

            newProperty = new PropertyDefinition(sourceProperty.Name, sourceProperty.Attributes, propertyType)
            {
                DeclaringType = targetType
            };
            targetType.Properties.Add(newProperty);

            MetadataBuilderHelper.CopyCustomAttributes(sourceProperty, newProperty, resolver);

            if (sourceProperty.GetMethod != null)
            {
                newProperty.GetMethod = targetType.InjectMethod(sourceProperty.GetMethod, resolver);
            }

            if (sourceProperty.SetMethod != null)
            {
                newProperty.SetMethod = targetType.InjectMethod(sourceProperty.SetMethod, resolver);
            }

            return newProperty;
        }