Exemple #1
0
        /// <summary>
        /// Creates a new constructor representation object.
        /// </summary>
        /// <param name="declaringType">Type declaring the member.</param>
        /// <param name="parameterTypes">Types of the parameters.</param>
        protected internal ConstructorInfoSlim(TypeSlim declaringType, ReadOnlyCollection <TypeSlim> parameterTypes)
            : base(declaringType)
        {
            Debug.Assert(parameterTypes != null);

            ParameterTypes = parameterTypes;
        }
 internal GenericTypeSlim4(GenericDefinitionTypeSlim typeDefinition, TypeSlim argument1, TypeSlim argument2, TypeSlim argument3, TypeSlim argument4)
     : base(typeDefinition, argument1)
 {
     _argument2 = argument2;
     _argument3 = argument3;
     _argument4 = argument4;
 }
Exemple #3
0
        public void Unify_GenericParameterOverride()
        {
            var gt = typeof(List <>).GetGenericArguments()[0];
            var gs = TypeSlim.GenericParameter("T");

            Assert.IsTrue(new MyUnifier().Unify(gt, gs));
        }
        /// <summary>
        /// Visits the specified type.
        /// </summary>
        /// <param name="type">Type to visit.</param>
        /// <returns>Result of the visit.</returns>
        public virtual TypeSlim Visit(TypeSlim type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (_types.TryGetValue(type, out TypeSlim res))
            {
                return(res);
            }

            res = type.Kind switch
            {
                TypeSlimKind.Simple => VisitSimple((SimpleTypeSlim)type),
                TypeSlimKind.Array => VisitArray((ArrayTypeSlim)type),
                TypeSlimKind.Structural => VisitStructural((StructuralTypeSlim)type),
                TypeSlimKind.GenericDefinition => VisitGenericDefinition((GenericDefinitionTypeSlim)type),
                TypeSlimKind.Generic => VisitGeneric((GenericTypeSlim)type),
                TypeSlimKind.GenericParameter => VisitGenericParameter((GenericParameterTypeSlim)type),
                _ => throw new NotSupportedException("Unknown type kind."),
            };

            _types[type] = res;
            return(res);
        }
        /// <summary>
        /// Visits the generic type arguments of a generic type.
        /// </summary>
        /// <param name="genericType">The generic type whose type arguments to visit.</param>
        /// <returns>Result of visiting the generic type arguments; null if no types changed.</returns>
        protected TypeSlim[] VisitGenericTypeArguments(GenericTypeSlim genericType)
        {
            var res = default(TypeSlim[]);

            var n = genericType.GenericArgumentCount;

            for (int i = 0; i < n; i++)
            {
                var oldNode = genericType.GetGenericArgument(i);
                var newNode = Visit(oldNode);

                if (res != null)
                {
                    res[i] = newNode;
                }
                else
                {
                    if ((object)oldNode != newNode)
                    {
                        res = new TypeSlim[n];

                        for (int j = 0; j < i; j++)
                        {
                            res[j] = genericType.GetGenericArgument(j);
                        }

                        res[i] = newNode;
                    }
                }
            }

            return(res);
        }
Exemple #6
0
        public void UnifyGenericParameter_Fail()
        {
            var unifier = new TypeUnifier();
            var openGenericParameterType = typeof(List <>).GetGenericArguments()[0];
            var genericParameterTypeSlim = TypeSlim.GenericParameter("T");

            unifier.Unify(openGenericParameterType, genericParameterTypeSlim);
        }
        /// <summary>
        /// Creates a new closed generic method representation object.
        /// </summary>
        /// <param name="declaringType">Type declaring the method.</param>
        /// <param name="name">Name of the method.</param>
        /// <param name="genericParameterTypes">Generic parameter types.</param>
        /// <param name="parameterTypes">Type of the method parameters.</param>
        /// <param name="returnType">Return type of the method.</param>
        internal GenericDefinitionMethodInfoSlim(TypeSlim declaringType, string name, ReadOnlyCollection <TypeSlim> genericParameterTypes, ReadOnlyCollection <TypeSlim> parameterTypes, TypeSlim returnType)
            : base(declaringType, name, parameterTypes, returnType)
        {
            Debug.Assert(genericParameterTypes != null);
            Debug.Assert(genericParameterTypes.Count >= 1);

            GenericParameterTypes = genericParameterTypes;
        }
Exemple #8
0
        /// <summary>
        /// Creates a new field representation object.
        /// </summary>
        /// <param name="declaringType">Type declaring the member.</param>
        /// <param name="name">Name of the field.</param>
        /// <param name="fieldType">Type of the field.</param>
        internal FieldInfoSlim(TypeSlim declaringType, string name, TypeSlim fieldType)
            : base(declaringType)
        {
            Debug.Assert(!string.IsNullOrEmpty(name));

            Name      = name;
            FieldType = fieldType;
        }
Exemple #9
0
        public override string Visit(TypeSlim typeSlim)
        {
            if (typeSlim == null)
            {
                return("null");
            }

            return(base.Visit(typeSlim));
        }
        /// <summary>
        /// Creates a new property representation object.
        /// </summary>
        /// <param name="declaringType">Type declaring the member.</param>
        /// <param name="name">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <param name="canWrite"><b>true</b> if property is writable, <b>false</b> otherwise.</param>
        internal PropertyInfoSlim(TypeSlim declaringType, string name, TypeSlim propertyType, bool canWrite)
            : base(declaringType)
        {
            Debug.Assert(!string.IsNullOrEmpty(name));

            Name         = name;
            PropertyType = propertyType;
            CanWrite     = canWrite;
        }
        /// <summary>
        /// Creates a new closed generic method representation object.
        /// </summary>
        /// <param name="declaringType">The declaring type of the generic method.</param>
        /// <param name="methodDefinition">Generic method definition.</param>
        /// <param name="arguments">Generic type arguments.</param>
        internal GenericMethodInfoSlim(TypeSlim declaringType, GenericDefinitionMethodInfoSlim methodDefinition, ReadOnlyCollection <TypeSlim> arguments)
            : base(declaringType)
        {
            Debug.Assert(methodDefinition != null);
            Debug.Assert(arguments != null);

            GenericMethodDefinition = methodDefinition;
            GenericArguments        = arguments;
        }
Exemple #12
0
        /// <summary>
        /// Creates a new simple method representation object.
        /// </summary>
        /// <param name="declaringType">Type declaring the method.</param>
        /// <param name="name">Name of the method.</param>
        /// <param name="parameterTypes">Type of the method parameters.</param>
        /// <param name="returnType">Return type of the method</param>
        protected SimpleMethodInfoSlimBase(TypeSlim declaringType, string name, ReadOnlyCollection <TypeSlim> parameterTypes, TypeSlim returnType)
            : base(declaringType)
        {
            Debug.Assert(!string.IsNullOrEmpty(name));
            Debug.Assert(parameterTypes != null);

            Name           = name;
            ParameterTypes = parameterTypes;
            ReturnType     = returnType;
        }
        /// <summary>
        /// Creates a new property representation object.
        /// </summary>
        /// <param name="declaringType">Type declaring the member.</param>
        /// <param name="name">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <param name="canWrite"><b>true</b> if property is writable, <b>false</b> otherwise.</param>
        /// <param name="indexParameterTypes">Types of the indexer parameters, if the property is indexed. Otherwise, an empty collection.</param>
        /// <returns>A new property representation object.</returns>
        internal static PropertyInfoSlim Make(TypeSlim declaringType, string name, TypeSlim propertyType, ReadOnlyCollection <TypeSlim> indexParameterTypes, bool canWrite)
        {
            Debug.Assert(indexParameterTypes != null);

            if (indexParameterTypes.Count == 0)
            {
                return(new PropertyInfoSlim(declaringType, name, propertyType, canWrite));
            }
            else
            {
                return(new IndexedPropertyInfoSlim(declaringType, name, propertyType, canWrite, indexParameterTypes));
            }
        }
        /// <summary>
        /// Computes the value for <see cref="ParameterTypes"/> with bound generic parameters.
        /// </summary>
        /// <param name="substitutor">The type substitutor to use for generic parameter binding.</param>
        /// <returns>The bound types to expose in <see cref="ParameterTypes"/>.</returns>
        private ReadOnlyCollection <TypeSlim> GetParameterTypes(TypeSlimSubstitutor substitutor)
        {
            var parameterTypesCount = GenericMethodDefinition.ParameterTypes.Count;

            var parameterTypes = new TypeSlim[parameterTypesCount];

            for (var i = 0; i < parameterTypesCount; ++i)
            {
                parameterTypes[i] = substitutor.Visit(GenericMethodDefinition.ParameterTypes[i]);
            }

            return(new TrueReadOnlyCollection <TypeSlim>(/* transfer ownership */ parameterTypes));
        }
        internal override GenericTypeSlim Rewrite(GenericDefinitionTypeSlim typeDefinition, TypeSlim[] arguments)
        {
            Debug.Assert(typeDefinition != null);
            Debug.Assert(arguments == null || arguments.Length == 2);

            if (arguments == null)
            {
                return(TypeSlim.Generic(typeDefinition, Argument1, _argument2));
            }
            else
            {
                return(TypeSlim.Generic(typeDefinition, arguments[0], arguments[1]));
            }
        }
Exemple #16
0
        internal override GenericTypeSlim Rewrite(GenericDefinitionTypeSlim typeDefinition, TypeSlim[] arguments)
        {
            Debug.Assert(typeDefinition != null);
            Debug.Assert(arguments == null || arguments.Length == GenericArgumentCount);

            // CONSIDER: Add a factory method overload with IEnumerable<TypeSlim>.

            if (arguments != null)
            {
                return(TypeSlim.Generic(typeDefinition, arguments));
            }
            else
            {
                return(TypeSlim.Generic(typeDefinition, GenericArguments));
            }
        }
        /// <summary>
        /// Visits the specified type.
        /// </summary>
        /// <param name="type">Type to visit.</param>
        /// <returns>Result of the visit.</returns>
        public virtual TType Visit(TypeSlim type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            return(type.Kind switch
            {
                TypeSlimKind.Simple => VisitSimple((SimpleTypeSlim)type),
                TypeSlimKind.Array => VisitArray((ArrayTypeSlim)type),
                TypeSlimKind.GenericDefinition => VisitGenericDefinition((GenericDefinitionTypeSlim)type),
                TypeSlimKind.Generic => VisitGeneric((GenericTypeSlim)type),
                TypeSlimKind.GenericParameter => VisitGenericParameter((GenericParameterTypeSlim)type),
                TypeSlimKind.Structural => VisitStructural((StructuralTypeSlim)type),
                _ => throw new NotSupportedException("Unknown type kind."),
            });
        /// <summary>
        /// Visits a collection of types.
        /// </summary>
        /// <param name="types">Types to visit.</param>
        /// <returns>Result of visiting the types.</returns>
        public ReadOnlyCollection <TypeSlim> Visit(ReadOnlyCollection <TypeSlim> types)
        {
            if (types == null)
            {
                throw new ArgumentNullException(nameof(types));
            }

            var res = default(TypeSlim[]);

            var n = types.Count;

            for (int i = 0; i < n; i++)
            {
                var oldNode = types[i];
                var newNode = Visit(oldNode);

                if (res != null)
                {
                    res[i] = newNode;
                }
                else
                {
                    if ((object)oldNode != newNode)
                    {
                        res = new TypeSlim[n];

                        for (int j = 0; j < i; j++)
                        {
                            res[j] = types[j];
                        }

                        res[i] = newNode;
                    }
                }
            }

            if (res != null)
            {
                return(new TrueReadOnlyCollection <TypeSlim>(/* transfer ownership */ res));
            }

            return(types);
        }
        /// <summary>
        /// Ensures that the values of properties <see cref="ReturnType"/> and <see cref="ParameterTypes"/> are
        /// computed and available in fields.
        /// </summary>
        private void EnsureProperties()
        {
            // REVIEW: Consider if we should compute ReturnType and ParameterTypes at the same time when these
            //         properties are triggering evaluation, or do it separately.

            if (_returnType == null || _parameterTypes == null)
            {
                var typeSubstitutor = GetSubstitutor();

                if (_returnType == null)
                {
                    _returnType = GetReturnType(typeSubstitutor);
                }

                if (_parameterTypes == null)
                {
                    _parameterTypes = GetParameterTypes(typeSubstitutor);
                }
            }
        }
Exemple #20
0
 protected abstract TypeSlim[] Pack(TypeSlim argument1);
Exemple #21
0
 public GenericTypeSlimN(GenericDefinitionTypeSlim typeDefinition, TypeSlim argument1)
     : base(typeDefinition)
 {
     _argument1 = argument1;
 }
Exemple #22
0
 /// <summary>
 /// Manually adds mapping from a CLR type to a slim type
 /// </summary>
 /// <param name="type">The CLR type.</param>
 /// <param name="typeSlim">The slim type.</param>
 public virtual void MapType(Type type, TypeSlim typeSlim)
 {
     TypeConverter.MapType(type, typeSlim);
 }
 /// <summary>
 /// Creates a new method representation object.
 /// </summary>
 /// <param name="declaringType">Type declaring the method.</param>
 protected MethodInfoSlim(TypeSlim declaringType)
     : base(declaringType)
 {
 }
 protected override TypeSlim[] Pack(TypeSlim argument1) => new TypeSlim[]
 {
     argument1, _argument2
 };
 internal GenericTypeSlim2(GenericDefinitionTypeSlim typeDefinition, TypeSlim argument1, TypeSlim argument2)
     : base(typeDefinition, argument1)
 {
     _argument2 = argument2;
 }
        /// <summary>
        /// Creates a new member of the specified member type.
        /// </summary>
        /// <param name="declaringType">Type declaring the member.</param>
        protected MemberInfoSlim(TypeSlim declaringType)
        {
            Debug.Assert(declaringType != null);

            DeclaringType = declaringType;
        }
 /// <summary>
 /// Manually adds a mapping from a slim type to a CLR type.
 /// </summary>
 /// <param name="typeSlim">The slim type.</param>
 /// <param name="type">The CLR type.</param>
 public void MapType(TypeSlim typeSlim, Type type)
 {
     _typeConverter.MapType(typeSlim, type);
 }
Exemple #28
0
        public void UnifySimple_DifferentAssemblyName_Fail()
        {
            var unifier = new TypeUnifier();

            unifier.Unify(typeof(int), TypeSlim.Simple(new AssemblySlim("Foo"), "System.Int32"));
        }
 internal GenericTypeSlim1(GenericDefinitionTypeSlim typeDefinition, TypeSlim argument1)
     : base(typeDefinition, argument1)
 {
 }
 /// <summary>
 /// Gets a CLR type from a slim type.
 /// </summary>
 /// <param name="type">The slim type.</param>
 /// <returns>The CLR type represented by the slim type.</returns>
 public Type ConvertType(TypeSlim type)
 {
     return(_typeConverter.Visit(type));
 }