tag a couple of obsolete members
[mcs.git] / class / System.ComponentModel.DataAnnotations / System.ComponentModel.DataAnnotations / EnumDataTypeAttribute.cs
bloba0efa1e846d6f9a67ef9c0b5b6c17381a434b3a8
1 //
2 // EnumDataTypeAttribute.cs
3 //
4 // Authors:
5 // Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell Inc. (http://novell.com)
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
30 #if NET_4_0
31 using System;
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)
51 Type type = EnumType;
53 if (!type.IsEnum)
54 throw new InvalidOperationException (
55 String.Format ("The type '{0}' needs to represent an enumeration type.", type.FullName)
58 if (value == null)
59 return true;
61 Type valueType = value.GetType ();
62 if (valueType.IsEnum && valueType != type)
63 return false;
65 string s = value as string;
66 if (s != null && s.Length == 0)
67 return true;
69 if (s != null && (valueType == typeof (bool) || valueType == typeof (char) || valueType == typeof (float)))
70 return false;
72 object o;
74 if (s != null) {
75 try {
76 o = Enum.Parse (type, s);
77 } catch {
78 return false;
80 } else if (valueType.IsEnum)
81 o = value;
82 else {
83 try {
84 o = Enum.ToObject (type, value);
85 } catch {
86 return false;
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
98 // enum:
100 // [Flags]
101 // enum Test
102 // {
103 // One = 1,
104 // Two = 2
105 // }
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);
117 #endif