Esempio n. 1
0
        public FieldReference ImportField(FieldReference field, ImportGenericContext context)
        {
            var declaring_type = ImportType(field.DeclaringType, context);

            context.Push(declaring_type);
            try
            {
                return new FieldReference
                {
                    Name = field.Name,
                    DeclaringType = declaring_type,
                    FieldType = ImportType(field.FieldType, context),
                };
            }
            finally
            {
                context.Pop();
            }
        }
        static FieldDefinition GetField(Collection<FieldDefinition> fields, FieldReference reference)
        {
            for (int i = 0; i < fields.Count; i++)
            {
                var field = fields[i];

                if (field.Name != reference.Name)
                    continue;

                if (!AreSame(field.FieldType, reference.FieldType))
                    continue;

                return field;
            }

            return null;
        }
        FieldDefinition GetField(TypeDefinition type, FieldReference reference)
        {
            while (type != null)
            {
                var field = GetField(type.Fields, reference);
                if (field != null)
                    return field;

                if (type.BaseType == null)
                    return null;

                type = Resolve(type.BaseType);
            }

            return null;
        }
        public virtual FieldDefinition Resolve(FieldReference field)
        {
            if (field == null)
                throw new ArgumentNullException("field");

            var type = Resolve(field.DeclaringType);
            if (type == null)
                return null;

            if (!type.HasFields)
                return null;

            return GetField(type, field);
        }
        private static FieldReference FixFieldImport(ModuleDefinition context, MethodDefinition source,
            MethodDefinition target, FieldReference field)
        {
            if (field.DeclaringType.FullName == source.DeclaringType.FullName)
                return FindMatchingField(target.DeclaringType, field);

            return context.Import(field);
        }
 /// <summary>
 /// Find a similar field in the given type definition 
 /// </summary>
 /// <param name="tdef">Type definition</param>
 /// <param name="fref">Field reference</param>
 /// <returns>Field definition (or null if not found)</returns>
 public static FieldDefinition FindMatchingField(TypeDefinition tdef, FieldReference fref)
 {
     return
         tdef.Fields.FirstOrDefault(fdef => (fdef.Name == fref.Name) && (fdef.FieldType.FullName == fref.FieldType.FullName));
 }