diff options
Diffstat (limited to 'src/System.Web.Mvc/ParameterInfoUtil.cs')
-rw-r--r-- | src/System.Web.Mvc/ParameterInfoUtil.cs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/System.Web.Mvc/ParameterInfoUtil.cs b/src/System.Web.Mvc/ParameterInfoUtil.cs new file mode 100644 index 00000000..e7343cdf --- /dev/null +++ b/src/System.Web.Mvc/ParameterInfoUtil.cs @@ -0,0 +1,33 @@ +using System.ComponentModel; +using System.Reflection; + +namespace System.Web.Mvc +{ + internal static class ParameterInfoUtil + { + public static bool TryGetDefaultValue(ParameterInfo parameterInfo, out object value) + { + // this will get the default value as seen by the VB / C# compilers + // if no value was baked in, RawDefaultValue returns DBNull.Value + object defaultValue = parameterInfo.DefaultValue; + if (defaultValue != DBNull.Value) + { + value = defaultValue; + return true; + } + + // if the compiler did not bake in a default value, check the [DefaultValue] attribute + DefaultValueAttribute[] attrs = (DefaultValueAttribute[])parameterInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false); + if (attrs == null || attrs.Length == 0) + { + value = default(object); + return false; + } + else + { + value = attrs[0].Value; + return true; + } + } + } +} |