Esempio n. 1
0
        /// <summary>
        /// Transform open generic types to closed instantiation using context information.
        /// As an example, if B{T} inherits from A{T}, running it with B{C} as context and A{B.T} as type, ti will return A{C}.
        /// </summary>
        public static TypeReference Process(TypeReference context, TypeReference type)
        {
            if (type == null)
            {
                return(null);
            }

            var genericInstanceTypeContext = context as GenericInstanceType;

            if (genericInstanceTypeContext == null)
            {
                return(type);
            }

            // Build dictionary that will map generic type to their real implementation type
            var resolvedType       = genericInstanceTypeContext.ElementType;
            var genericTypeMapping = new Dictionary <TypeReference, TypeReference>();

            for (int i = 0; i < resolvedType.GenericParameters.Count; ++i)
            {
                var genericParameter = genericInstanceTypeContext.ElementType.GenericParameters[i];
                genericTypeMapping.Add(genericParameter, genericInstanceTypeContext.GenericArguments[i]);
            }

            var visitor = new ResolveGenericsVisitor(genericTypeMapping);
            var result  = visitor.VisitDynamic(type);

            // Make sure type is closed now
            if (result.ContainsGenericParameter())
            {
                throw new InvalidOperationException("Unsupported generic resolution.");
            }

            return(result);
        }
Esempio n. 2
0
        public static TypeReference Process(MethodReference context, TypeReference type, bool resolveOnlyReferences = false)
        {
            if (type == null)
            {
                return(null);
            }

            if (context == null)
            {
                return(type);
            }

            // Visit recursively and replace generic parameters with generic arguments from context
            var genericInstanceTypeContext   = context.DeclaringType as GenericInstanceType;
            var genericInstanceMethodContext = context as GenericInstanceMethod;

            if (genericInstanceMethodContext == null && genericInstanceTypeContext == null)
            {
                return(type);
            }

            var visitor = new ResolveGenericsVisitor(context, resolveOnlyReferences);
            var result  = visitor.VisitDynamic(type);

            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Transform open generic types to closed instantiation using context information.
        /// As an example, if B{T} inherits from A{T}, running it with B{C} as context and A{B.T} as type, ti will return A{C}.
        /// </summary>
        public static TypeReference Process(TypeReference context, TypeReference type)
        {
            if (type == null)
            {
                return(null);
            }

            var genericInstanceTypeContext = context as GenericInstanceType;

            if (genericInstanceTypeContext == null)
            {
                return(type);
            }

            // Visit recursively and replace generic parameters with generic arguments from context
            var visitor = new ResolveGenericsVisitor(context);
            var result  = visitor.VisitDynamic(type);

            // Make sure type is closed now
            if (result.ContainsGenericParameter)
            {
                throw new InvalidOperationException("Unsupported generic resolution.");
            }

            return(result);
        }
Esempio n. 4
0
        public static TypeReference Process(MethodReference context, TypeReference type)
        {
            if (type == null)
            {
                return(null);
            }

            if (context == null)
            {
                return(type);
            }

            var genericInstanceTypeContext   = context.DeclaringType as GenericInstanceType;
            var genericInstanceMethodContext = context as GenericInstanceMethod;

            if (genericInstanceMethodContext == null && genericInstanceTypeContext == null)
            {
                return(type);
            }

            // Build dictionary that will map generic type to their real implementation type
            var genericTypeMapping = new Dictionary <TypeReference, TypeReference>(TypeReferenceComparer.Default);

            if (genericInstanceTypeContext != null)
            {
                var resolvedType = genericInstanceTypeContext.ElementType;
                for (int i = 0; i < resolvedType.GenericParameters.Count; ++i)
                {
                    var genericParameter = genericInstanceTypeContext.ElementType.GenericParameters[i];
                    genericTypeMapping.Add(genericParameter, genericInstanceTypeContext.GenericArguments[i]);
                }
            }

            if (genericInstanceMethodContext != null)
            {
                var elementMethod  = genericInstanceMethodContext.ElementMethod;
                var resolvedMethod = genericInstanceMethodContext.Resolve();
                for (int i = 0; i < elementMethod.GenericParameters.Count; ++i)
                {
                    var genericParameter = elementMethod.GenericParameters[i];
                    genericTypeMapping.Add(genericParameter, genericInstanceMethodContext.GenericArguments[i]);

                    var genericParameter2 = resolvedMethod.GenericParameters[i];
                    if (genericParameter != genericParameter2)
                    {
                        genericTypeMapping.Add(genericParameter2, genericInstanceMethodContext.GenericArguments[i]);
                    }
                }
            }

            var visitor = new ResolveGenericsVisitor(genericTypeMapping);
            var result  = visitor.VisitDynamic(type);

            // Make sure type is closed now
            //if (result.ContainsGenericParameter())
            //    throw new InvalidOperationException("Unsupported generic resolution.");

            return(result);
        }
        public static TypeReference Process(MethodReference context, TypeReference type, bool resolveOnlyReferences = false)
        {
            if (type == null)
                return null;

            if (context == null)
                return type;

            // Visit recursively and replace generic parameters with generic arguments from context
            var genericInstanceTypeContext = context.DeclaringType as GenericInstanceType;
            var genericInstanceMethodContext = context as GenericInstanceMethod;
            if (genericInstanceMethodContext == null && genericInstanceTypeContext == null)
                return type;

            var visitor = new ResolveGenericsVisitor(context, resolveOnlyReferences);
            var result = visitor.VisitDynamic(type);

            return result;
        }
        /// <summary>
        /// Transform open generic types to closed instantiation using context information.
        /// As an example, if B{T} inherits from A{T}, running it with B{C} as context and A{B.T} as type, ti will return A{C}.
        /// </summary>
        public static TypeReference Process(TypeReference context, TypeReference type)
        {
            if (type == null)
                return null;

            var genericInstanceTypeContext = context as GenericInstanceType;
            if (genericInstanceTypeContext == null)
                return type;

            // Visit recursively and replace generic parameters with generic arguments from context
            var visitor = new ResolveGenericsVisitor(context);
            var result = visitor.VisitDynamic(type);

            // Make sure type is closed now
            if (result.ContainsGenericParameter)
                throw new InvalidOperationException("Unsupported generic resolution.");

            return result;
        }