Beispiel #1
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));
        }
Beispiel #2
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));
        }
 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;
     }
 }