Exemple #1
0
        public static Func <int, object> CreateCollectionInstanceFactory(Type abstractType, Type targetType, Type comparisonType)
#endif // !UNITY
        {
            var constructor = UnpackHelpers.GetCollectionConstructor(targetType);
            var parameters = constructor.GetParameters();

            switch (parameters.Length)
            {
            case 0:
            {
                return(_ =>
#if !UNITY
                       (T)
#endif // !UNITY
                       constructor.InvokePreservingExceptionType());
            }

            case 1:
            {
                if (parameters[0].ParameterType == typeof(int))
                {
                    return(capacity =>
#if !UNITY
                           (T)
#endif // !UNITY
                           constructor.InvokePreservingExceptionType(capacity));
                }
                else if (UnpackHelpers.IsIEqualityComparer(parameters[0].ParameterType))
                {
                    var comparer =
#if !UNITY
                        EqualityComparer <TKey> .Default;
#else
                        UnpackHelpers.GetEqualityComparer(comparisonType);
#endif // !UNITY
                    return(_ =>
#if !UNITY
                           (T)
#endif // !UNITY
                           constructor.InvokePreservingExceptionType(comparer));
                }

                break;
            }

            case 2:
            {
                var comparer =
#if !UNITY
                    EqualityComparer <TKey> .Default;
#else
                    UnpackHelpers.GetEqualityComparer(comparisonType);
#endif // !UNITY
                if (parameters[0].ParameterType == typeof(int) &&
                    UnpackHelpers.IsIEqualityComparer(parameters[1].ParameterType))
                {
                    return(capacity =>
#if !UNITY
                           (T)
#endif // !UNITY
                           constructor.InvokePreservingExceptionType(capacity, comparer));
                }
                else if (UnpackHelpers.IsIEqualityComparer(parameters[0].ParameterType) &&
                         parameters[0].ParameterType == typeof(int))
                {
                    return(capacity =>
#if !UNITY
                           (T)
#endif // !UNITY
                           constructor.InvokePreservingExceptionType(comparer, capacity));
                }

                break;
            }
            }

            throw SerializationExceptions.NewTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity(
#if !UNITY
                      typeof(T)
#else
                      abstractType
#endif // !UNITY
                      );
        }