TypeAllowsNullValue() public static method

public static TypeAllowsNullValue ( Type type ) : bool
type System.Type
return bool
Exemplo n.º 1
0
        internal static object ExtractParameterFromDictionary(ParameterInfo parameterInfo, IDictionary <string, object> parameters, MethodInfo methodInfo)
        {
            object value;

            if (!parameters.TryGetValue(parameterInfo.Name, out value))
            {
                // the key should always be present, even if the parameter value is null
                string message = String.Format(CultureInfo.CurrentUICulture, MvcResources.AsyncActionDescriptor_ParameterNotInDictionary,
                                               parameterInfo.Name, parameterInfo.ParameterType, methodInfo, methodInfo.DeclaringType);
                throw new ArgumentException(message, "parameters");
            }

            if (value == null && !TypeHelpers.TypeAllowsNullValue(parameterInfo.ParameterType))
            {
                // tried to pass a null value for a non-nullable parameter type
                string message = String.Format(CultureInfo.CurrentUICulture, MvcResources.AsyncActionDescriptor_ParameterCannotBeNull,
                                               parameterInfo.Name, parameterInfo.ParameterType, methodInfo, methodInfo.DeclaringType);
                throw new ArgumentException(message, "parameters");
            }

            if (value != null && !parameterInfo.ParameterType.IsInstanceOfType(value))
            {
                // value was supplied but is not of the proper type
                string message = String.Format(CultureInfo.CurrentUICulture, MvcResources.AsyncActionDescriptor_ParameterValueHasWrongType,
                                               parameterInfo.Name, methodInfo, methodInfo.DeclaringType, value.GetType(), parameterInfo.ParameterType);
                throw new ArgumentException(message, "parameters");
            }

            return(value);
        }
        private static object ExtractParameterOrDefaultFromDictionary(ParameterInfo parameterInfo, IDictionary <string, object> parameters)
        {
            Type parameterType = parameterInfo.ParameterType;

            object value;

            parameters.TryGetValue(parameterInfo.Name, out value);

            // if wrong type, replace with default instance
            if (parameterType.IsInstanceOfType(value))
            {
                return(value);
            }
            else
            {
                return((TypeHelpers.TypeAllowsNullValue(parameterType)) ? null : Activator.CreateInstance(parameterType));
            }
        }