private void Visit(FieldDefinition field, GenericContext context)
 {
     try
     {
         this.Visit((ICustomAttributeProvider)field, context);
         field.FieldType = base.ModuleContext.Retarget(field.FieldType, context);
     }
     catch
     {
         Console.Error.WriteLine("Failed to fix references for field {0}", field.FullName);
         throw;
     }
 }
 private void Visit(PropertyDefinition property, GenericContext context)
 {
     try
     {
         this.Visit((ICustomAttributeProvider)property, context);
         property.PropertyType = base.ModuleContext.Retarget(property.PropertyType, context);
     }
     catch
     {
         Console.Error.WriteLine("Failed to fix references for property {0}", property.FullName);
         throw;
     }
 }
 private void Visit(EventDefinition @event, GenericContext context)
 {
     try
     {
         this.Visit((ICustomAttributeProvider)@event, context);
         @event.EventType = base.ModuleContext.Retarget(@event.EventType, context);
     }
     catch
     {
         Console.Error.WriteLine("Failed to fix references for event {0}", @event.FullName);
         throw;
     }
 }
Example #4
0
        public MethodReference Retarget(MethodReference method, GenericContext context)
        {
            MethodReference reference;

            if (method.IsDefinition && Utility.SameScope(method.DeclaringType.Scope, this.Module))
            {
                reference = method;
            }
            else if (method.IsGenericInstance)
            {
                GenericInstanceMethod method2 = (GenericInstanceMethod)method;
                GenericInstanceMethod method3 = new GenericInstanceMethod(this.Retarget(method2.ElementMethod, context));
                foreach (TypeReference reference2 in method2.GenericArguments)
                {
                    TypeReference item = this.Retarget(reference2, context);
                    method3.GenericArguments.Add(item);
                }
                reference = method3;
            }
            else
            {
                TypeReference declaringType = this.Retarget(method.DeclaringType, context);
                context.PushType(declaringType.GetElementType());
                MethodReference owner = new MethodReference(method.Name, method.ReturnType, declaringType)
                {
                    CallingConvention = method.CallingConvention,
                    ExplicitThis      = method.ExplicitThis,
                    HasThis           = method.HasThis
                };
                using (Collection <GenericParameter> .Enumerator enumerator2 = method.GenericParameters.GetEnumerator())
                {
                    while (enumerator2.MoveNext())
                    {
                        GenericParameter parameter = new GenericParameter(enumerator2.Current.Name, owner);
                        owner.GenericParameters.Add(parameter);
                    }
                }
                context.PushMethod(owner);
                owner.ReturnType = this.Retarget(method.ReturnType, context);
                foreach (ParameterDefinition definition in method.Parameters)
                {
                    TypeReference       parameterType = this.Retarget(definition.ParameterType, context);
                    ParameterDefinition definition2   = new ParameterDefinition(definition.Name, definition.Attributes, parameterType);
                    owner.Parameters.Add(definition2);
                }
                reference = owner;
                context.PopMethod();
                context.PopType();
            }
            return(this.Module.ImportReference(reference));
        }
 private void Visit(Collection <GenericParameter> genericParameters, GenericContext context)
 {
     foreach (GenericParameter parameter in genericParameters)
     {
         for (int i = 0; i < parameter.Constraints.Count; i++)
         {
             try
             {
                 parameter.Constraints[i] = base.ModuleContext.Retarget(parameter.Constraints[i], context);
             }
             catch
             {
                 Console.Error.WriteLine("Failed to fix references for generic parameter {0}", parameter.FullName);
                 throw;
             }
         }
     }
 }
Example #6
0
        public FieldReference Retarget(FieldReference field, GenericContext context)
        {
            FieldReference reference;

            if (field.IsDefinition)
            {
                reference = field;
            }
            else
            {
                TypeReference declaringType = this.Retarget(field.DeclaringType, context);
                context.PushType(declaringType.GetElementType());
                TypeReference fieldType = this.Retarget(field.FieldType, context);
                context.PopType();
                reference = new FieldReference(field.Name, fieldType, declaringType);
            }
            return(this.Module.ImportReference(reference));
        }
 private void Visit(ICustomAttributeProvider provider, GenericContext context)
 {
     using (Collection <CustomAttribute> .Enumerator enumerator = provider.CustomAttributes.GetEnumerator())
     {
         while (enumerator.MoveNext())
         {
             CustomAttribute customAttribute = enumerator.Current;
             try
             {
                 MethodDefinition method = customAttribute.AttributeType.Resolve().Methods.Single <MethodDefinition>(m => m.FullName == customAttribute.Constructor.FullName);
                 customAttribute.Constructor = base.ModuleContext.Retarget(method, context);
                 for (int i = 0; i < customAttribute.ConstructorArguments.Count; i++)
                 {
                     CustomAttributeArgument argument = customAttribute.ConstructorArguments[i];
                     customAttribute.ConstructorArguments[i] = base.ModuleContext.Retarget(argument, context);
                 }
                 for (int j = 0; j < customAttribute.Fields.Count; j++)
                 {
                     CustomAttributeNamedArgument argument2 = customAttribute.Fields[j];
                     CustomAttributeArgument      argument3 = base.ModuleContext.Retarget(argument2.Argument, context);
                     customAttribute.Fields[j] = new CustomAttributeNamedArgument(argument2.Name, argument3);
                 }
                 for (int k = 0; k < customAttribute.Properties.Count; k++)
                 {
                     CustomAttributeNamedArgument argument4 = customAttribute.Properties[k];
                     CustomAttributeArgument      argument5 = base.ModuleContext.Retarget(argument4.Argument, context);
                     customAttribute.Properties[k] = new CustomAttributeNamedArgument(argument4.Name, argument5);
                 }
                 continue;
             }
             catch
             {
                 Console.Error.WriteLine("Failed to fix references for attribute {0}", customAttribute.AttributeType.FullName);
                 throw;
             }
         }
     }
 }
 public void Visit(TypeDefinition type)
 {
     try
     {
         GenericContext context = new GenericContext();
         context.PushType(type);
         this.Visit(type, context);
         type.BaseType = base.ModuleContext.Retarget(type.BaseType, context);
         for (int i = 0; i < type.Interfaces.Count; i++)
         {
             type.Interfaces[i] = base.ModuleContext.Retarget(type.Interfaces[i], context);
         }
         this.Visit(type.GenericParameters, context);
         foreach (FieldDefinition definition in type.Fields)
         {
             this.Visit(definition, context);
         }
         foreach (MethodDefinition definition2 in type.Methods)
         {
             this.Visit(definition2, context);
         }
         foreach (PropertyDefinition definition3 in type.Properties)
         {
             this.Visit(definition3, context);
         }
         foreach (EventDefinition definition4 in type.Events)
         {
             this.Visit(definition4, context);
         }
         context.PopType();
     }
     catch
     {
         Console.Error.WriteLine("Failed to fix references for type {0}", type.FullName);
         throw;
     }
 }
 private void Visit(MethodDefinition method, GenericContext context)
 {
     try
     {
         context.PushMethod(method);
         this.Visit((ICustomAttributeProvider)method, context);
         this.Visit(method.GenericParameters, context);
         this.Visit(method.MethodReturnType, context);
         method.ReturnType = base.ModuleContext.Retarget(method.ReturnType, context);
         for (int i = 0; i < method.Parameters.Count; i++)
         {
             this.Visit(method.Parameters[i], context);
             method.Parameters[i].ParameterType = base.ModuleContext.Retarget(method.Parameters[i].ParameterType, context);
         }
         for (int j = 0; j < method.Overrides.Count; j++)
         {
             method.Overrides[j] = base.ModuleContext.Retarget(method.Overrides[j], context);
         }
         if (method.HasBody)
         {
             foreach (ExceptionHandler handler in method.Body.ExceptionHandlers)
             {
                 handler.CatchType = base.ModuleContext.Retarget(handler.CatchType, context);
             }
             foreach (VariableDefinition definition in method.Body.Variables)
             {
                 definition.VariableType = base.ModuleContext.Retarget(definition.VariableType, context);
             }
             foreach (Instruction instruction in method.Body.Instructions)
             {
                 MemberReference operand = instruction.Operand as MemberReference;
                 if (operand != null)
                 {
                     MethodReference reference3 = operand as MethodReference;
                     if (reference3 == null)
                     {
                         FieldReference field = operand as FieldReference;
                         if (field == null)
                         {
                             TypeReference type = operand as TypeReference;
                             if (type == null)
                             {
                                 throw new NotImplementedException();
                             }
                             instruction.Operand = base.ModuleContext.Retarget(type, context);
                         }
                         else
                         {
                             instruction.Operand = base.ModuleContext.Retarget(field, context);
                         }
                     }
                     else
                     {
                         instruction.Operand = base.ModuleContext.Retarget(reference3, context);
                     }
                 }
                 else
                 {
                     ParameterReference reference2 = instruction.Operand as ParameterReference;
                     if (reference2 != null)
                     {
                         reference2.ParameterType = base.ModuleContext.Retarget(reference2.ParameterType, context);
                     }
                 }
             }
             context.PopMethod();
         }
     }
     catch
     {
         Console.Error.WriteLine("Failed to fix references for method {0}", method.FullName);
         throw;
     }
 }
Example #10
0
        public TypeReference Retarget(TypeReference type, GenericContext context)
        {
            TypeReference reference;

            if (type == null)
            {
                return(type);
            }
            if ((type.IsFunctionPointer || type.IsOptionalModifier) || type.IsSentinel)
            {
                throw new NotImplementedException();
            }
            if (type.IsArray)
            {
                ArrayType type2 = (ArrayType)type;
                ArrayType type3 = new ArrayType(this.Retarget(type2.ElementType, context), type2.Rank);
                for (int i = 0; i < type2.Dimensions.Count; i++)
                {
                    ArrayDimension dimension = type2.Dimensions[i];
                    type3.Dimensions[i] = new ArrayDimension(dimension.LowerBound, dimension.UpperBound);
                }
                reference = type3;
            }
            else if (type.IsByReference)
            {
                ByReferenceType type4 = (ByReferenceType)type;
                reference = new ByReferenceType(this.Retarget(type4.ElementType, context));
            }
            else if (type.IsDefinition && Utility.SameScope(type.Scope, this.Module))
            {
                reference = type;
            }
            else if (type.IsGenericInstance)
            {
                GenericInstanceType type5 = (GenericInstanceType)type;
                GenericInstanceType type6 = new GenericInstanceType(this.Retarget(type5.ElementType, context));
                foreach (TypeReference reference2 in type5.GenericArguments)
                {
                    TypeReference item = this.Retarget(reference2, context);
                    type6.GenericArguments.Add(item);
                }
                reference = type6;
            }
            else
            {
                if (type.IsGenericParameter)
                {
                    GenericParameter parameter = (GenericParameter)type;
                    return(context.Retarget(parameter));
                }
                if (type.IsPinned)
                {
                    PinnedType type7 = (PinnedType)type;
                    reference = new PinnedType(this.Retarget(type7.ElementType, context));
                }
                else if (type.IsPointer)
                {
                    PointerType type8 = (PointerType)type;
                    reference = new PointerType(this.Retarget(type8.ElementType, context));
                }
                else if (type.IsRequiredModifier)
                {
                    RequiredModifierType type9 = (RequiredModifierType)type;
                    reference = new RequiredModifierType(this.Retarget(type9.ModifierType, context), this.Retarget(type9.ElementType, context));
                }
                else
                {
                    reference = type.Resolve();
                    if ((reference == null) && (this.OperationContext.Platform == Platform.UAP))
                    {
                        string fullName = null;
                        string str2     = type.FullName;
                        if ((!(str2 == "System.Collections.ArrayList") && !(str2 == "System.Collections.CollectionBase")) && (!(str2 == "System.Collections.Hashtable") && !(str2 == "System.Collections.Stack")))
                        {
                            if (str2 == "System.Reflection.BindingFlags")
                            {
                                fullName = "System.Reflection.TypeExtensions";
                            }
                        }
                        else
                        {
                            fullName = "System.Collections.NonGeneric";
                        }
                        if (fullName != null)
                        {
                            IMetadataScope scope = type.Scope;
                            type.Scope = AssemblyNameReference.Parse(fullName);
                            reference  = type.Resolve();
                            type.Scope = scope;
                        }
                    }
                }
            }
            return(this.Module.ImportReference(reference));
        }