コード例 #1
0
        public Constructor Close([NotNull] Type[] typeClosures)
        {
            if (typeClosures == null)
            {
                throw new ArgumentNullException("typeClosures");
            }

            // Check input arrays are valid.
            if (typeClosures.Length != ExtendedType.GenericArguments.Count())
            {
                return(null);
            }

            // If we haven't got any type closures, we can return this constructor.
            if (typeClosures.All(t => t == null))
            {
                return(this);
            }

            // Close type
            ExtendedType et = ExtendedType.CloseType(typeClosures);

            // Check closure succeeded.
            if (et == null)
            {
                return(null);
            }

            // Create new search.
            Debug.Assert(_parameters.Value != null);
            int pCount = _parameters.Value.Length;

            TypeSearch[] searchTypes          = new TypeSearch[pCount + 1];
            Type[]       typeGenericArguments = et.GenericArguments.Select(g => g.Type).ToArray();
            // Search for closed
            for (int i = 0; i < pCount; i++)
            {
                Debug.Assert(_parameters.Value[i] != null);
                Type pType = _parameters.Value[i].ParameterType;
                Debug.Assert(pType != null);
                searchTypes[i] = Reflection.ExpandParameterType(pType, Array <Type> .Empty, typeGenericArguments);
            }

            // Add return type
            searchTypes[pCount] = et.Type;

            // Search for constructor on new type.
            return(et.GetConstructor(searchTypes));
        }
コード例 #2
0
        public Constructor Close([NotNull] Type[] typeClosures)
        {
            if (typeClosures == null) throw new ArgumentNullException("typeClosures");

            // Check input arrays are valid.
            if (typeClosures.Length != ExtendedType.GenericArguments.Count())
                return null;

            // If we haven't got any type closures, we can return this constructor.
            if (typeClosures.All(t => t == null))
                return this;

            // Close type
            ExtendedType et = ExtendedType.CloseType(typeClosures);

            // Check closure succeeded.
            if (et == null)
                return null;

            // Create new search.
            Debug.Assert(_parameters.Value != null);
            int pCount = _parameters.Value.Length;
            TypeSearch[] searchTypes = new TypeSearch[pCount + 1];
            Type[] typeGenericArguments = et.GenericArguments.Select(g => g.Type).ToArray();
            // Search for closed 
            for (int i = 0; i < pCount; i++)
            {
                Debug.Assert(_parameters.Value[i] != null);
                Type pType = _parameters.Value[i].ParameterType;
                Debug.Assert(pType != null);
                searchTypes[i] = Reflection.ExpandParameterType(pType, Array<Type>.Empty, typeGenericArguments);
            }

            // Add return type
            searchTypes[pCount] = et.Type;

            // Search for constructor on new type.
            return et.GetConstructor(searchTypes);
        }
コード例 #3
0
ファイル: Method.cs プロジェクト: webappsuk/CoreLibraries
        public Method Close([NotNull] Type[] typeClosures, [NotNull] Type[] signatureClosures)
        {
            if (typeClosures == null) throw new ArgumentNullException("typeClosures");
            if (signatureClosures == null) throw new ArgumentNullException("signatureClosures");

            Debug.Assert(_genericArguments.Value != null);

            // Check input arrays are valid.
            if ((typeClosures.Length != ExtendedType.GenericArguments.Count()) ||
                (signatureClosures.Length != _genericArguments.Value.Count()))
                return null;

            // If we have any type closures then we need to close the type and look for the method on there.
            if (typeClosures.Any(t => t != null))
            {
                // Close type
                ExtendedType et = ExtendedType.CloseType(typeClosures);

                // Check closure succeeded.
                if (et == null)
                    return null;

                // Create new search.
                Debug.Assert(_parameters.Value != null);
                int pCount = _parameters.Value.Length;
                TypeSearch[] searchTypes = new TypeSearch[pCount + 1];
                Type[] typeGenericArguments = et.GenericArguments.Select(g => g.Type).ToArray();
                // Search for closed 
                for (int i = 0; i < pCount; i++)
                {
                    Debug.Assert(_parameters.Value[i] != null);
                    Type pType = _parameters.Value[i].ParameterType;
                    Debug.Assert(pType != null);
                    searchTypes[i] = Reflection.ExpandParameterType(pType, signatureClosures, typeGenericArguments);
                }

                // Add return type
                Type rType = Info.ReturnType;
                searchTypes[pCount] = Reflection.ExpandParameterType(rType, signatureClosures, typeGenericArguments);

                // Search for method on new type.
                return et.GetMethod(Info.Name, signatureClosures.Length, searchTypes);
            }

            // Substitute missing types with concrete ones.
            int closures = signatureClosures.Length;
            Type[] gta = new Type[closures];
            for (int i = 0; i < closures; i++)
                gta[i] = signatureClosures[i] ?? _genericArguments.Value[i].Type;

            // Create closed method, cache it and return.
            // ReSharper disable once AssignNullToNotNullAttribute
            string key = string.Join("|", gta.Select(t => ExtendedType.Get(t).Signature));

            Debug.Assert(_closedMethods.Value != null);
            return _closedMethods.Value.GetOrAdd(
                key,
                k =>
                {
                    try
                    {
                        return new Method(ExtendedType, Info.MakeGenericMethod(gta));
                    }
                    catch (ArgumentException)
                    {
                        return null;
                    }
                });
        }
コード例 #4
0
ファイル: Method.cs プロジェクト: webappsuk/CoreLibraries
        public Method Close([NotNull] Type[] typeClosures, [NotNull] Type[] signatureClosures)
        {
            if (typeClosures == null)
            {
                throw new ArgumentNullException("typeClosures");
            }
            if (signatureClosures == null)
            {
                throw new ArgumentNullException("signatureClosures");
            }

            Debug.Assert(_genericArguments.Value != null);

            // Check input arrays are valid.
            if ((typeClosures.Length != ExtendedType.GenericArguments.Count()) ||
                (signatureClosures.Length != _genericArguments.Value.Count()))
            {
                return(null);
            }

            // If we have any type closures then we need to close the type and look for the method on there.
            if (typeClosures.Any(t => t != null))
            {
                // Close type
                ExtendedType et = ExtendedType.CloseType(typeClosures);

                // Check closure succeeded.
                if (et == null)
                {
                    return(null);
                }

                // Create new search.
                Debug.Assert(_parameters.Value != null);
                int          pCount               = _parameters.Value.Length;
                TypeSearch[] searchTypes          = new TypeSearch[pCount + 1];
                Type[]       typeGenericArguments = et.GenericArguments.Select(g => g.Type).ToArray();
                // Search for closed
                for (int i = 0; i < pCount; i++)
                {
                    Debug.Assert(_parameters.Value[i] != null);
                    Type pType = _parameters.Value[i].ParameterType;
                    Debug.Assert(pType != null);
                    searchTypes[i] = Reflection.ExpandParameterType(pType, signatureClosures, typeGenericArguments);
                }

                // Add return type
                Type rType = Info.ReturnType;
                searchTypes[pCount] = Reflection.ExpandParameterType(rType, signatureClosures, typeGenericArguments);

                // Search for method on new type.
                return(et.GetMethod(Info.Name, signatureClosures.Length, searchTypes));
            }

            // Substitute missing types with concrete ones.
            int closures = signatureClosures.Length;

            Type[] gta = new Type[closures];
            for (int i = 0; i < closures; i++)
            {
                gta[i] = signatureClosures[i] ?? _genericArguments.Value[i].Type;
            }

            // Create closed method, cache it and return.
            // ReSharper disable once AssignNullToNotNullAttribute
            string key = string.Join("|", gta.Select(t => ExtendedType.Get(t).Signature));

            Debug.Assert(_closedMethods.Value != null);
            return(_closedMethods.Value.GetOrAdd(
                       key,
                       k =>
            {
                try
                {
                    return new Method(ExtendedType, Info.MakeGenericMethod(gta));
                }
                catch (ArgumentException)
                {
                    return null;
                }
            }));
        }