internal static CustomAttribute Clone(CustomAttribute custattr, ImportContext context)
        {
            CustomAttribute ca = new CustomAttribute(context.Import(custattr.Constructor));

            custattr.CopyTo(ca);
            return(ca);
        }
        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);
        }
 static void CloneConstraints(GenericParameter gp, GenericParameter ngp, ImportContext context)
 {
     if (gp.HasConstraints)
     {
         foreach (TypeReference constraint in gp.Constraints)
         {
             ngp.Constraints.Add(context.Import(constraint));
         }
     }
 }
        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);
        }