Exemple #1
0
        private static IEnumerable <TypeReference> GetGenericInstances(this GenericInstanceType type)
        {
            var result = new List <TypeReference>();

            result.Add(type);

            var resolved = type.BetterResolve();
            var genericArgumentsNames = resolved.GenericParameters.Select(x => x.FullName).ToArray();
            var genericArguments      = type.GenericArguments.ToArray();

            if (resolved.BaseType != null)
            {
                result.AddRange(resolved.BaseType.GetGenericInstances(genericArgumentsNames, genericArguments));
            }

            if (resolved.Interfaces != null && resolved.Interfaces.Count > 0)
            {
                foreach (var item in resolved.Interfaces)
                {
                    result.AddRange(item.InterfaceType.GetGenericInstances(genericArgumentsNames, genericArguments));
                }
            }

            return(result);
        }
Exemple #2
0
        public static IReadOnlyDictionary <string, TypeReference> GetGenericResolvedTypeNames(this GenericInstanceType type)
        {
            var genericArgumentNames          = type.BetterResolve().GenericParameters.Select(x => x.FullName).ToArray();
            var genericArgumentsOfCurrentType = type.GenericArguments.ToArray();
            var baseType = type as TypeReference;

            var result = new Dictionary <string, TypeReference>();

            while (baseType != null)
            {
                if (baseType.IsGenericInstance)
                {
                    var genericType = baseType as GenericInstanceType;
                    if (genericType != null)
                    {
                        genericArgumentNames          = genericType.BetterResolve().GenericParameters.Select(x => x.FullName).ToArray();
                        genericArgumentsOfCurrentType = genericType.GenericArguments.ToArray();

                        for (int i = 0; i < genericArgumentNames.Length; i++)
                        {
                            if (!result.ContainsKey(genericArgumentNames[i]))
                            {
                                result.Add(genericArgumentNames[i], genericArgumentsOfCurrentType[i]);
                            }
                        }
                    }
                }

                baseType = baseType.BetterResolve().BaseType;
            }

            return(new ReadOnlyDictionary <string, TypeReference>(result));
        }
Exemple #3
0
        public static IReadOnlyDictionary<string, TypeReference> GetGenericResolvedTypeName(this GenericInstanceType type)
        {
            var genericArgumentNames = type.BetterResolve().GenericParameters.Select(x => x.FullName).ToArray();
            var genericArgumentsOfCurrentType = type.GenericArguments.ToArray();

            var result = new Dictionary<string, TypeReference>();

            for (int i = 0; i < genericArgumentNames.Length; i++)
                result.Add(genericArgumentNames[i], genericArgumentsOfCurrentType[i]);

            return new ReadOnlyDictionary<string, TypeReference>(result);
        }
Exemple #4
0
        public static GenericInstanceType ResolveGenericArguments(this GenericInstanceType self, GenericInstanceType inheritingOrImplementingType)
        {
            if (self.FullName == inheritingOrImplementingType.FullName)
                return self;

            var genericParameters = inheritingOrImplementingType.GetGenericResolvedTypeName();
            var genericArguments = new TypeReference[self.GenericArguments.Count];

            for (int i = 0; i < genericArguments.Length; i++)
                genericArguments[i] = genericParameters.ContainsKey(self.GenericArguments[i].FullName) ? genericParameters[self.GenericArguments[i].FullName] : self.GenericArguments[i];

            return self.BetterResolve().MakeGenericInstanceType(genericArguments);
        }