Beispiel #1
0
        internal static FieldDefinition Clone(FieldDefinition field, ImportContext context)
        {
            FieldDefinition nf = new FieldDefinition(
                field.Name,
                context.Import(field.FieldType),
                field.Attributes);

            if (field.HasConstant)
            {
                nf.Constant = field.Constant;
            }
            if (field.MarshalSpec != null)
            {
                nf.MarshalSpec = field.MarshalSpec.CloneInto(nf);
            }
            if (field.RVA != RVA.Zero)
            {
                nf.InitialValue = field.InitialValue;
            }
            else
            {
                nf.InitialValue = new byte [0];
            }
            if (field.HasLayoutInfo)
            {
                nf.Offset = field.Offset;
            }

            foreach (CustomAttribute ca in field.CustomAttributes)
            {
                nf.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
            }

            return(nf);
        }
        internal static GenericParameter Clone(GenericParameter gp, ImportContext context)
        {
            GenericParameter ngp;

            if (gp.Owner is TypeReference)
            {
                ngp = new GenericParameter(gp.m_name, context.GenericContext.Type);
            }
            else if (gp.Owner is MethodReference)
            {
                ngp = new GenericParameter(gp.m_name, context.GenericContext.Method);
            }
            else
            {
                throw new NotSupportedException();
            }

            ngp.Position   = gp.Owner.GenericParameters.IndexOf(gp);
            ngp.Attributes = gp.Attributes;

            if (gp.HasCustomAttributes)
            {
                foreach (CustomAttribute ca in gp.CustomAttributes)
                {
                    ngp.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
                }
            }

            return(ngp);
        }
        internal static PropertyDefinition Clone(PropertyDefinition prop, ImportContext context)
        {
            PropertyDefinition np = new PropertyDefinition(
                prop.Name,
                context.Import(prop.PropertyType),
                prop.Attributes);

            if (prop.HasConstant)
            {
                np.Constant = prop.Constant;
            }

            if (context.GenericContext.Type is TypeDefinition)
            {
                TypeDefinition type = context.GenericContext.Type as TypeDefinition;
                if (prop.SetMethod != null)
                {
                    np.SetMethod = type.Methods.GetMethod(prop.SetMethod.Name, prop.SetMethod.Parameters);
                }
                if (prop.GetMethod != null)
                {
                    np.GetMethod = type.Methods.GetMethod(prop.GetMethod.Name, prop.GetMethod.Parameters);
                }
            }

            foreach (CustomAttribute ca in prop.CustomAttributes)
            {
                np.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
            }

            return(np);
        }
        internal static MethodDefinition Clone(MethodDefinition meth, ImportContext context)
        {
            MethodDefinition nm = new MethodDefinition(
                meth.Name,
                RVA.Zero,
                meth.Attributes,
                meth.ImplAttributes,
                meth.HasThis,
                meth.ExplicitThis,
                meth.CallingConvention);

            MethodReference contextMethod = context.GenericContext.Method;

            context.GenericContext.Method = nm;

            GenericParameter.CloneInto(meth, nm, context);

            nm.ReturnType.ReturnType = context.Import(meth.ReturnType.ReturnType);

            if (meth.ReturnType.Parameter != null)
            {
                nm.ReturnType.Parameter        = ParameterDefinition.Clone(meth.ReturnType.Parameter, context);
                nm.ReturnType.Parameter.Method = nm;
            }

            if (meth.PInvokeInfo != null)
            {
                nm.PInvokeInfo = meth.PInvokeInfo;                 // TODO: import module ?
            }
            if (meth.HasParameters)
            {
                foreach (ParameterDefinition param in meth.Parameters)
                {
                    nm.Parameters.Add(ParameterDefinition.Clone(param, context));
                }
            }
            if (meth.HasOverrides)
            {
                foreach (MethodReference ov in meth.Overrides)
                {
                    nm.Overrides.Add(context.Import(ov));
                }
            }
            if (meth.HasCustomAttributes)
            {
                foreach (CustomAttribute ca in meth.CustomAttributes)
                {
                    nm.CustomAttributes.Add(CustomAttribute.Clone(ca, context));
                }
            }
            if (meth.HasSecurityDeclarations)
            {
                foreach (SecurityDeclaration sec in meth.SecurityDeclarations)
                {
                    nm.SecurityDeclarations.Add(SecurityDeclaration.Clone(sec));
                }
            }

            if (meth.Body != null)
            {
                nm.Body = MethodBody.Clone(meth.Body, nm, context);
            }

            context.GenericContext.Method = contextMethod;

            return(nm);
        }