Пример #1
0
            public override TypeReference ImportTypeReference(TypeReference t, ImportContext context)
            {
                try
                {
                    if (t.Module == Module)
                        return t;

                    TypeReference type;
                    if (Handler.TryGetReplacement(t, out type))
                    {
                        // generic?
                        GenericInstanceType genericInstanceType = t as GenericInstanceType;

                        if (genericInstanceType != null) // yes generic!
                        {
                            // add generic parameters and import generic arguments
                            GenericInstanceType replacementGenericInstanceType = new GenericInstanceType(type);
                            foreach (GenericParameter genericParameter in genericInstanceType.GenericParameters)
                            {
                                replacementGenericInstanceType.GenericParameters.Add(genericParameter);
                            }
                            foreach (TypeReference genericArgument in genericInstanceType.GenericArguments)
                            {
                                replacementGenericInstanceType.GenericArguments.Add(ImportTypeReference(genericArgument, context));
                            }
                            t = replacementGenericInstanceType;
                        }
                        else // not generic.
                        {
                            t = type;
                        }
                    }

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

                    if (t is GenericParameter)
                    {
                        // TODO: HACK!!!
                        return t;
                        //return GetGenericParameter((GenericParameter)t, context);
                    }

                    type = 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 if (t.Namespace == "System")
                    {
                        // TODO: Use lookup table rather than checking for "System" namespace
                        asm = Handler._mscorlib;
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }

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

                    context.PushGenericContext(type);

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

                    context.PopGenericContext();

                    Module.TypeReferences.Add(type);
                    return type;
                }
                catch (Exception e)
                {
                    throw new CompilerException("Unable to import type reference " + t + ".  Import context is " + context + ".", e);
                }
            }
Пример #2
0
            public override MethodReference ImportMethodReference(MethodReference mr, ImportContext context)
            {
                if (mr.DeclaringType.Module == 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,
                    null,
                    null,
                    mr.HasThis,
                    mr.ExplicitThis,
                    mr.CallingConvention);
                meth.DeclaringType = ImportTypeReference(mr.DeclaringType, context);

                //TypeReference contextType = meth.DeclaringType.GetOriginalType();
                TypeReference contextType = meth.DeclaringType;

                context.PushGenericContext(contextType, meth);

                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)));

                context.PopGenericContext();

                Module.MemberReferences.Add(meth);
                return meth;
            }