Пример #1
0
        public MethodReference ImportMethodReference(MethodReference mr, ImportContext context)
        {
            if (mr.DeclaringType.Module == m_module)
            {
                return(mr);
            }

            ImportCache();

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

            MethodReference meth = m_memberRefCache [mr.ToString()] as MethodReference;

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

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

            TypeReference contextType = meth.DeclaringType;

            while (contextType is TypeSpecification)
            {
                contextType = (contextType as TypeSpecification).ElementType;
            }

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

            foreach (GenericParameter gp in mr.GenericParameters)
            {
                meth.GenericParameters.Add(GenericParameter.Clone(gp, context));
            }

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

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

            m_module.MemberReferences.Add(meth);
            m_memberRefCache [mr.ToString()] = meth;
            return(meth);
        }
Пример #2
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);

            context.GenericContext.Method = nm;

            foreach (GenericParameter p in meth.GenericParameters)
            {
                nm.GenericParameters.Add(GenericParameter.Clone(p, context));
            }

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

            if (meth.ReturnType.HasConstant)
            {
                nm.ReturnType.Constant = meth.ReturnType.Constant;
            }

            if (meth.ReturnType.MarshalSpec != null)
            {
                nm.ReturnType.MarshalSpec = meth.ReturnType.MarshalSpec;
            }

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

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

            return(nm);
        }
Пример #3
0
        public TypeReference ImportTypeReference(TypeReference t, ImportContext context)
        {
            if (t.Module == m_module)
            {
                return(t);
            }

            ImportCache();

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

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

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

            if (type != null)
            {
                return(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();
            }

            type = new TypeReference(t.Name, t.Namespace, asm, t.IsValueType);

            context.GenericContext.Type = type;

            foreach (GenericParameter gp in t.GenericParameters)
            {
                type.GenericParameters.Add(GenericParameter.Clone(gp, context));
            }

            m_module.TypeReferences.Add(type);
            return(type);
        }
        internal static TypeDefinition Clone(TypeDefinition type, ImportContext context)
        {
            TypeDefinition nt = new TypeDefinition(
                type.Name,
                type.Namespace,
                type.Attributes);

            context.GenericContext.Type = nt;

            foreach (GenericParameter p in type.GenericParameters)
            {
                nt.GenericParameters.Add(GenericParameter.Clone(p, context));
            }

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

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

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

            return(nt);
        }