Exemplo n.º 1
0
            public CecilType Get(TypeReference reference)
            {
                if (reference.GetType() == typeof(TypeReference))
                {
                    reference = reference.Resolve();
                }
                var definition = reference.Resolve();
                var asm        = TypeSystem.FindAsm(definition.Module.Assembly);

                if (!_definitions.TryGetValue(definition, out var dentry))
                {
                    _definitions[definition] = dentry = new DefinitionEntry();
                }
                if (reference is TypeDefinition def)
                {
                    return(dentry.Direct ?? (dentry.Direct = new CecilType(TypeSystem, asm, def)));
                }

                var rtype = reference.GetType();

                if (!dentry.References.TryGetValue(rtype, out var rlist))
                {
                    dentry.References[rtype] = rlist = new List <CecilType>();
                }
                var found = rlist.FirstOrDefault(t => CecilHelpers.Equals(t.Reference, reference));

                if (found != null)
                {
                    return(found);
                }
                var rv = new CecilType(TypeSystem, asm, definition, reference);

                rlist.Add(rv);
                return(rv);
            }
Exemplo n.º 2
0
        public static TypeReference TransformGeneric(this TypeReference reference, TypeReference declaringType)
        {
            try
            {
                _recursionDepth++;
                if (_recursionDepth > 50)
                {
                    throw new StackOverflowException();
                }
                // I'm most likely missing something here...
                if (declaringType is GenericInstanceType inst)
                {
                    if (reference is GenericParameter g)
                    {
                        var ga = inst.GenericArguments[g.Position];
                        if (ga == g)
                        {
                            return(ga);
                        }
                        return(ga.TransformGeneric(declaringType));
                    }
                    else if (reference is GenericInstanceType gref)
                    {
                        GenericInstanceType clone = null;
                        for (var c = 0; c < gref.GenericArguments.Count; c++)
                        {
                            var arg = gref.GenericArguments[c];
                            if (arg is GenericParameter genericParameter &&
                                CecilHelpers.Equals(genericParameter.DeclaringType, inst.ElementType))
                            {
                                if (clone == null)
                                {
                                    clone = new GenericInstanceType(gref.ElementType);
                                    foreach (var orig in gref.GenericArguments)
                                    {
                                        clone.GenericArguments.Add(orig);
                                    }
                                }

                                clone.GenericArguments[c] = inst.GenericArguments[genericParameter.Position];
                            }
                        }

                        return(clone ?? gref);
                    }
                }

                return(reference);
            }

            finally
            {
                _recursionDepth--;
            }
        }
Exemplo n.º 3
0
 public bool Equals(IXamlIlType other)
 {
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     if (!(other is CecilType o))
     {
         return(false);
     }
     return(CecilHelpers.Equals(Reference, o.Reference));
 }