TypeMissingDefaultConstructor() static private method

ArgumentException with message like "Type '{0}' does not have a default constructor"
static private TypeMissingDefaultConstructor ( object p0, string paramName ) : Exception
p0 object
paramName string
return Exception
示例#1
0
 /// <summary>
 /// Creates a <see cref="NewExpression"/> that represents calling the parameterless constructor of the specified type.
 /// </summary>
 /// <param name="type">A <see cref="Type"/> that has a constructor that takes no arguments. </param>
 /// <returns>A <see cref="NewExpression"/> that has the <see cref="NodeType"/> property equal to New and the Constructor property set to the ConstructorInfo that represents the parameterless constructor of the specified type.</returns>
 public static NewExpression New(Type type)
 {
     ContractUtils.RequiresNotNull(type, "type");
     if (type == typeof(void))
     {
         throw Error.ArgumentCannotBeOfTypeVoid();
     }
     if (!type.IsValueType)
     {
         var constructorInfo = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).SingleOrDefault(c => c.GetParameters().Length == 0);
         if (constructorInfo == null)
         {
             throw Error.TypeMissingDefaultConstructor(type);
         }
         return(New(constructorInfo));
     }
     return(new NewValueTypeExpression(type, EmptyCollection <Expression> .Instance, null));
 }
示例#2
0
        /// <summary>
        /// Creates a <see cref="NewExpression"/> that represents calling the parameterless constructor of the specified type.
        /// </summary>
        /// <param name="type">A <see cref="Type"/> that has a constructor that takes no arguments.</param>
        /// <returns>A <see cref="NewExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="ExpressionType.New"/> and the <see cref="NewExpression.Constructor"/> property set to the <see cref="ConstructorInfo"/> that represents the parameterless constructor of the specified type.</returns>
        public static NewExpression New(Type type)
        {
            ContractUtils.RequiresNotNull(type, nameof(type));
            if (type == typeof(void))
            {
                throw Error.ArgumentCannotBeOfTypeVoid(nameof(type));
            }
            TypeUtils.ValidateType(type, nameof(type));

            if (!type.GetTypeInfo().IsValueType)
            {
                ConstructorInfo ci = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).SingleOrDefault(c => c.GetParametersCached().Length == 0);
                if (ci == null)
                {
                    throw Error.TypeMissingDefaultConstructor(type, nameof(type));
                }
                return(New(ci));
            }
            return(new NewValueTypeExpression(type, EmptyReadOnlyCollection <Expression> .Instance, null));
        }
示例#3
0
        //CONFORMING
        public static NewExpression New(Type type)
        {
            ContractUtils.RequiresNotNull(type, "type");
            if (type == typeof(void))
            {
                throw Error.ArgumentCannotBeOfTypeVoid();
            }
            ConstructorInfo ci = null;

            if (!type.IsValueType)
            {
                ci = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, System.Type.EmptyTypes, null);
                if (ci == null)
                {
                    throw Error.TypeMissingDefaultConstructor(type);
                }
                return(New(ci));
            }
            return(new NewValueTypeExpression(type, EmptyReadOnlyCollection <Expression> .Instance, null));
        }
示例#4
0
        /// <summary>
        /// Creates a <see cref="NewExpression"/> that represents calling the parameterless constructor of the specified type.
        /// </summary>
        /// <param name="type">A <see cref="Type"/> that has a constructor that takes no arguments. </param>
        /// <returns>A <see cref="NewExpression"/> that has the <see cref="NodeType"/> property equal to New and the Constructor property set to the ConstructorInfo that represents the parameterless constructor of the specified type.</returns>
        public static NewExpression New(Type type)
        {
            ContractUtils.RequiresNotNull(type, "type");
            if (type == typeof(void))
            {
                throw Error.ArgumentCannotBeOfTypeVoid();
            }
            ConstructorInfo ci = null;

            if (!type.GetTypeInfo().IsValueType)
            {
                ci = type.GetConstructor(Array.Empty <Type>());
                if (ci == null)
                {
                    throw Error.TypeMissingDefaultConstructor(type);
                }
                return(New(ci));
            }
            return(new NewValueTypeExpression(type, EmptyReadOnlyCollection <Expression> .Instance, null));
        }