forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatorOptions.cs
More file actions
115 lines (99 loc) · 4.62 KB
/
Copy pathValidatorOptions.cs
File metadata and controls
115 lines (99 loc) · 4.62 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#region License
// Copyright (c) Jeremy Skinner (http://www.jeremyskinner.co.uk)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The latest version of this file can be found at http://www.codeplex.com/FluentValidation
#endregion
namespace ServiceStack.FluentValidation
{
using System;
#if !WINDOWS_PHONE
using System.ComponentModel;
//using System.ComponentModel.DataAnnotations;
#endif
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Internal;
public static class ValidatorOptions {
public static CascadeMode CascadeMode = CascadeMode.Continue;
public static Type ResourceProviderType;
private static Func<Type, MemberInfo, LambdaExpression, string> propertyNameResolver = DefaultPropertyNameResolver;
private static Func<Type, MemberInfo, LambdaExpression, string> displayNameResolver = DefaultDisplayNameResolver;
public static Func<Type, MemberInfo, LambdaExpression, string> PropertyNameResolver {
get { return propertyNameResolver; }
set { propertyNameResolver = value ?? DefaultPropertyNameResolver; }
}
public static Func<Type, MemberInfo, LambdaExpression, string> DisplayNameResolver {
get { return displayNameResolver; }
set { displayNameResolver = value ?? DefaultDisplayNameResolver; }
}
static string DefaultPropertyNameResolver(Type type, MemberInfo memberInfo, LambdaExpression expression) {
if (expression != null) {
var chain = PropertyChain.FromExpression(expression);
if (chain.Count > 0) return chain.ToString();
}
if (memberInfo != null) {
return memberInfo.Name;
}
return null;
}
static string DefaultDisplayNameResolver(Type type, MemberInfo memberInfo, LambdaExpression expression) {
if (memberInfo == null) return null;
return GetDisplayName(memberInfo);
/*string name = null;
#if !WINDOWS_PHONE
var displayAttribute = (DisplayAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(DisplayAttribute));
if(displayAttribute != null) {
name = displayAttribute.GetName();
}
#endif
#if !SILVERLIGHT
// Silverlight doesn't have DisplayAttribute.
if(string.IsNullOrEmpty(name)) {
// Couldn't find a name from a DisplayAttribute. Try DisplayNameAttribute instead.
var displayNameAttribute = (DisplayNameAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(DisplayNameAttribute));
if(displayNameAttribute != null) {
name = displayNameAttribute.DisplayName;
}
}
#endif
return name;*/
}
// Nasty hack to work around not referencing DataAnnotations directly.
// At some point investigate the DataAnnotations reference issue in more detail and go back to using the code above.
static string GetDisplayName(MemberInfo member) {
var attributes = (from attr in member.GetCustomAttributes(true)
select new {attr, type = attr.GetType()}).ToList();
string name = null;
#if !WINDOWS_PHONE
name = (from attr in attributes
where attr.type.Name == "DisplayAttribute"
let method = attr.type.GetMethod("GetName", BindingFlags.Instance | BindingFlags.Public)
where method != null
select method.Invoke(attr.attr, null) as string).FirstOrDefault();
#endif
#if !SILVERLIGHT
if (string.IsNullOrEmpty(name)) {
name = (from attr in attributes
where attr.type.Name == "DisplayNameAttribute"
let property = attr.type.GetProperty("DisplayName", BindingFlags.Instance | BindingFlags.Public)
where property != null
select property.GetValue(attr.attr, null) as string).FirstOrDefault();
}
#endif
return name;
}
}
}