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);
        }
Example #2
0
 static TypeDefinition ImportTypeDefinition(TypeDefinition type, ImportContext context)
 {
     return(TypeDefinition.Clone(type, context));
 }
Example #3
0
 static MethodDefinition ImportMethodDefinition(MethodDefinition meth, ImportContext context)
 {
     return(MethodDefinition.Clone(meth, context));
 }
Example #4
0
 static FieldDefinition ImportFieldDefinition(FieldDefinition field, ImportContext context)
 {
     return(FieldDefinition.Clone(field, context));
 }
 internal static void CloneInto(IGenericParameterProvider old, IGenericParameterProvider np, ImportContext context)
 {
     foreach (GenericParameter gp in old.GenericParameters)
     {
         GenericParameter ngp = Clone(gp, context);
         np.GenericParameters.Add(ngp);
         CloneConstraints(gp, ngp, context);
     }
 }
Example #6
0
        TypeSpecification GetTypeSpec(TypeSpecification original, ImportContext context)
        {
            TypeSpecification typeSpec;

            TypeReference elementType = ImportTypeReference(original.ElementType, context);

            if (original is PointerType)
            {
                typeSpec = new PointerType(elementType);
            }
            else if (original is ArrayType)                 // deal with complex arrays
            {
                typeSpec = new ArrayType(elementType);
            }
            else if (original is ReferenceType)
            {
                typeSpec = new ReferenceType(elementType);
            }
            else if (original is GenericInstanceType)
            {
                GenericInstanceType git         = original as GenericInstanceType;
                GenericInstanceType genElemType = new GenericInstanceType(elementType);

                context.GenericContext.CheckProvider(genElemType.GetOriginalType(), git.GenericArguments.Count);
                foreach (TypeReference arg in git.GenericArguments)
                {
                    genElemType.GenericArguments.Add(ImportTypeReference(arg, context));
                }

                typeSpec = genElemType;
            }
            else if (original is ModifierOptional)
            {
                TypeReference mt = (original as ModifierOptional).ModifierType;
                typeSpec = new ModifierOptional(elementType, ImportTypeReference(mt, context));
            }
            else if (original is ModifierRequired)
            {
                TypeReference mt = (original as ModifierRequired).ModifierType;
                typeSpec = new ModifierRequired(elementType, ImportTypeReference(mt, context));
            }
            else if (original is SentinelType)
            {
                typeSpec = new SentinelType(elementType);
            }
            else if (original is FunctionPointerType)
            {
                FunctionPointerType ori = original as FunctionPointerType;

                FunctionPointerType fnptr = new FunctionPointerType(
                    ori.HasThis,
                    ori.ExplicitThis,
                    ori.CallingConvention,
                    new MethodReturnType(ImportTypeReference(ori.ReturnType.ReturnType, context)));

                foreach (ParameterDefinition parameter in ori.Parameters)
                {
                    fnptr.Parameters.Add(new ParameterDefinition(ImportTypeReference(parameter.ParameterType, context)));
                }

                typeSpec = fnptr;
            }
            else
            {
                throw new ReflectionException("Unknown element type: {0}", original.GetType().Name);
            }

            return(typeSpec);
        }