Пример #1
0
 public FieldDefinition InsertField(FieldDefinition field, bool clone)
 {
     if (fields.ContainsKey(field.Name))
     {
         Console.Error.WriteLine("Error: duplicated field {0}", field.Name);
         return(null);
     }
     else
     {
         if (clone)
         {
             field = field.Clone();
         }
         fields [field.Name] = field;
         return(field);
     }
 }
        internal static TypeDefinition Clone(TypeDefinition type, ImportContext context)
        {
            TypeDefinition nt = new TypeDefinition(
                type.Name,
                type.Namespace,
                type.Attributes);

            TypeReference contextType = context.GenericContext.Type;

            context.GenericContext.Type = nt;

            GenericParameter.CloneInto(type, nt, context);

            if (type.BaseType != null)
            {
                nt.BaseType = context.Import(type.BaseType);
            }

            if (type.HasLayoutInfo)
            {
                nt.ClassSize   = type.ClassSize;
                nt.PackingSize = type.PackingSize;
            }

            if (type.HasFields)
            {
                foreach (FieldDefinition field in type.Fields)
                {
                    nt.Fields.Add(FieldDefinition.Clone(field, context));
                }
            }
            if (type.HasConstructors)
            {
                foreach (MethodDefinition ctor in type.Constructors)
                {
                    nt.Constructors.Add(MethodDefinition.Clone(ctor, context));
                }
            }
            if (type.HasMethods)
            {
                foreach (MethodDefinition meth in type.Methods)
                {
                    nt.Methods.Add(MethodDefinition.Clone(meth, context));
                }
            }
            if (type.HasEvents)
            {
                foreach (EventDefinition evt in type.Events)
                {
                    nt.Events.Add(EventDefinition.Clone(evt, context));
                }
            }
            if (type.HasProperties)
            {
                foreach (PropertyDefinition prop in type.Properties)
                {
                    nt.Properties.Add(PropertyDefinition.Clone(prop, context));
                }
            }
            if (type.HasInterfaces)
            {
                foreach (TypeReference intf in type.Interfaces)
                {
                    nt.Interfaces.Add(context.Import(intf));
                }
            }
            if (type.HasNestedTypes)
            {
                foreach (TypeDefinition nested in type.NestedTypes)
                {
                    nt.NestedTypes.Add(Clone(nested, context));
                }
            }
            if (type.HasCustomAttributes)
            {
                foreach (CustomAttribute ca in type.CustomAttributes)
                {
                    nt.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
                }
            }
            if (type.HasSecurityDeclarations)
            {
                foreach (SecurityDeclaration dec in type.SecurityDeclarations)
                {
                    nt.SecurityDeclarations.Add(SecurityDeclaration.Clone(dec));
                }
            }

            context.GenericContext.Type = contextType;

            return(nt);
        }