コード例 #1
0
        public virtual TypeReference ImportTypeReference(TypeReference t, ImportContext context)
        {
            if (t.Module == m_module)
            {
                return(t);
            }

            if (t is TypeSpecification)
            {
                return(GetTypeSpec(t as TypeSpecification, context));
            }

            if (t is GenericParameter)
            {
                return(GetGenericParameter(t as GenericParameter, context));
            }

            TypeReference type = m_module.TypeReferences [t.FullName];

            if (type != null)
            {
                return(AdjustReference(t, type));
            }

            AssemblyNameReference asm;

            if (t.Scope is AssemblyNameReference)
            {
                asm = ImportAssembly((AssemblyNameReference)t.Scope);
            }
            else if (t.Scope is ModuleDefinition)
            {
                asm = ImportAssembly(((ModuleDefinition)t.Scope).Assembly.Name);
            }
            else
            {
                throw new NotImplementedException();
            }

            if (t.DeclaringType != null)
            {
                type = new TypeReference(t.Name, string.Empty, asm, t.IsValueType);
                type.DeclaringType = ImportTypeReference(t.DeclaringType, context);
            }
            else
            {
                type = new TypeReference(t.Name, t.Namespace, asm, t.IsValueType);
            }

            TypeReference contextType = context.GenericContext.Type;

            context.GenericContext.Type = type;

            GenericParameter.CloneInto(t, type, context);

            context.GenericContext.Type = contextType;

            m_module.TypeReferences.Add(type);
            return(type);
        }
コード例 #2
0
        public virtual MethodReference ImportMethodReference(MethodReference mr, ImportContext context)
        {
            if (mr.DeclaringType.Module == m_module)
            {
                return(mr);
            }

            if (mr is MethodSpecification)
            {
                return(GetMethodSpec(mr, context));
            }

            MethodReference meth = (MethodReference)GetMemberReference(mr);

            if (meth != null)
            {
                return(meth);
            }

            meth = new MethodReference(
                mr.Name,
                mr.HasThis,
                mr.ExplicitThis,
                mr.CallingConvention);
            meth.DeclaringType = ImportTypeReference(mr.DeclaringType, context);

            TypeReference   contextType   = context.GenericContext.Type;
            MethodReference contextMethod = context.GenericContext.Method;

            context.GenericContext.Method = meth;
            context.GenericContext.Type   = meth.DeclaringType.GetOriginalType();

            GenericParameter.CloneInto(mr, meth, context);

            meth.ReturnType.ReturnType = ImportTypeReference(mr.ReturnType.ReturnType, context);

            foreach (ParameterDefinition param in mr.Parameters)
            {
                meth.Parameters.Add(new ParameterDefinition(
                                        ImportTypeReference(param.ParameterType, context)));
            }

            context.GenericContext.Type   = contextType;
            context.GenericContext.Method = contextMethod;

            m_module.MemberReferences.Add(meth);
            return(meth);
        }
コード例 #3
0
        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);
        }