Merge pull request #301 from directhex/master
[mono.git] / mcs / class / System.Web.Mvc3 / Mvc / ParameterInfoUtil.cs
1 namespace System.Web.Mvc {
2     using System;
3     using System.ComponentModel;
4     using System.Reflection;
5
6     internal static class ParameterInfoUtil {
7
8         public static bool TryGetDefaultValue(ParameterInfo parameterInfo, out object value) {
9             // this will get the default value as seen by the VB / C# compilers
10             // if no value was baked in, RawDefaultValue returns DBNull.Value
11             object defaultValue = parameterInfo.DefaultValue;
12             if (defaultValue != DBNull.Value) {
13                 value = defaultValue;
14                 return true;
15             }
16
17             // if the compiler did not bake in a default value, check the [DefaultValue] attribute
18             DefaultValueAttribute[] attrs = (DefaultValueAttribute[])parameterInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false);
19             if (attrs == null || attrs.Length == 0) {
20                 value = default(object);
21                 return false;
22             }
23             else {
24                 value = attrs[0].Value;
25                 return true;
26             }
27         }
28
29     }
30 }