2 // EnumDataTypeAttribute.cs
5 // Marek Habersack <mhabersack@novell.com>
7 // Copyright (C) 2010 Novell Inc. (http://novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Collections
.Generic
;
33 using System
.ComponentModel
;
34 using System
.Globalization
;
36 namespace System
.ComponentModel
.DataAnnotations
38 [AttributeUsage (AttributeTargets
.Method
| AttributeTargets
.Property
| AttributeTargets
.Field
| AttributeTargets
.Parameter
, AllowMultiple
= false)]
39 public sealed class EnumDataTypeAttribute
: DataTypeAttribute
41 public Type EnumType { get; private set; }
43 public EnumDataTypeAttribute (Type enumType
)
44 : base (DataType
.Custom
)
46 this.EnumType
= enumType
;
49 public override bool IsValid (object value)
54 throw new InvalidOperationException (
55 String
.Format ("The type '{0}' needs to represent an enumeration type.", type
.FullName
)
61 Type valueType
= value.GetType ();
62 if (valueType
.IsEnum
&& valueType
!= type
)
65 string s
= value as string;
66 if (s
!= null && s
.Length
== 0)
69 if (s
!= null && (valueType
== typeof (bool) || valueType
== typeof (char) || valueType
== typeof (float)))
76 o
= Enum
.Parse (type
, s
);
80 } else if (valueType
.IsEnum
)
84 o
= Enum
.ToObject (type
, value);
90 object[] attrs
= type
.GetCustomAttributes (typeof (FlagsAttribute
), true);
91 if (attrs
!= null && attrs
.Length
> 0) {
92 string sval
= Convert
.ChangeType (o
, Enum
.GetUnderlyingType (type
), CultureInfo
.InvariantCulture
).ToString ();
94 // This looks weird, but what happens here is that if we have a
95 // mismatch, the above type change will make sval equal o.ToString
96 // () and if we have a match, then sval will be string
97 // representation of the enum member's value. So, if we have an
107 // And the passed value was 3, then o.ToString () == "One, Two" and
108 // sval == "3". If the passed value was 33, though, o.ToString () ==
109 // "33" and sval == "33" - thus we DON'T have a match.
110 return !sval
.Equals (o
.ToString ());
113 return Enum
.IsDefined (type
, o
);