forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterInfoExtensions.cs
More file actions
41 lines (37 loc) · 1.41 KB
/
ParameterInfoExtensions.cs
File metadata and controls
41 lines (37 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.ComponentModel;
using System.Reflection;
namespace System.Web.Http.Internal
{
internal static class ParameterInfoExtensions
{
public static bool TryGetDefaultValue(this ParameterInfo parameterInfo, out object value)
{
if (parameterInfo == null)
{
throw Error.ArgumentNull("parameterInfo");
}
// 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;
}
}
}
}