ThrowTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity() 정적인 개인적인 메소드

static private ThrowTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity ( Type type ) : void
type System.Type
리턴 void
예제 #1
0
        internal static SerializationTarget DetermineCollectionSerializationStrategy(Type instanceType, bool allowAsymmetricSerializer)
        {
            bool canDeserialize;
            var  collectionConstructor = UnpackHelpers.TryGetCollectionConstructor(instanceType);

            if (collectionConstructor == null)
            {
                if (!allowAsymmetricSerializer)
                {
                    SerializationExceptions.ThrowTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity(instanceType);
                }

                // Pack only.
                canDeserialize = false;
            }
            else
            {
                canDeserialize = true;
            }

            return(SerializationTarget.CreateForCollection(collectionConstructor, canDeserialize));
        }
예제 #2
0
        /// <summary>
        ///		Retrieves a most appropriate constructor with <see cref="Int32"/> capacity parameter and <see cref="IEqualityComparer{T}"/> comparer parameter or both of them, >or default constructor of the <paramref name="instanceType"/>.
        /// </summary>
        /// <param name="instanceType">The target collection type to be instanciated.</param>
        /// <returns>A constructor of the <paramref name="instanceType"/>.</returns>
        internal static ConstructorInfo GetCollectionConstructor(Type instanceType)
        {
            const int noParameters            = 0;
            const int withCapacity            = 10;
            const int withComparer            = 11;
            const int withComparerAndCapacity = 20;
            const int withCapacityAndComparer = 21;

            ConstructorInfo constructor  = null;
            var             currentScore = -1;

            foreach (var candidate in instanceType.GetConstructors())
            {
                var parameters = candidate.GetParameters();
                switch (parameters.Length)
                {
                case 0:
                {
                    if (currentScore < noParameters)
                    {
                        constructor  = candidate;
                        currentScore = noParameters;
                    }

                    break;
                }

                case 1:
                {
                    if (currentScore < withCapacity && parameters[0].ParameterType == typeof(int))
                    {
                        constructor  = candidate;
                        currentScore = noParameters;
                    }
                    else if (currentScore < withComparer && IsIEqualityComparer(parameters[0].ParameterType))
                    {
                        constructor  = candidate;
                        currentScore = noParameters;
                    }
                    break;
                }

                case 2:
                {
                    if (currentScore < withCapacityAndComparer && parameters[0].ParameterType == typeof(int) && IsIEqualityComparer(parameters[1].ParameterType))
                    {
                        constructor  = candidate;
                        currentScore = withCapacityAndComparer;
                    }
                    else if (currentScore < withComparerAndCapacity && parameters[1].ParameterType == typeof(int) && IsIEqualityComparer(parameters[0].ParameterType))
                    {
                        constructor  = candidate;
                        currentScore = withComparerAndCapacity;
                    }

                    break;
                }
                }
            }

            if (constructor == null)
            {
                SerializationExceptions.ThrowTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity(instanceType);
            }

            return(constructor);
        }