5 // David Stone <david@gixug.com>
7 // Copyright (C) 2010 David Stone
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.ComponentModel;
32 namespace System.ComponentModel.DataAnnotations
35 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Method, AllowMultiple = false)]
36 public sealed class DisplayAttribute : Attribute
38 public Type ResourceType { get; set; }
40 public string Description { get; set; }
41 public string GroupName { get; set; }
42 public string Name { get; set; }
43 public string ShortName { get; set; }
44 public string Prompt { get; set; }
46 const string property_not_set_message = "The {0} property has not been set. Use the Get{0} method to get the value.";
47 const string localization_failed_message = "Cannot retrieve property '{0}' because localization failed. Type '{1} is not public or does not contain a public static string property with the name '{2}'.";
49 bool? _autoGenerateField;
50 public bool AutoGenerateField {
52 if (!_autoGenerateField.HasValue) {
53 throw new InvalidOperationException (string.Format (property_not_set_message, "AutoGenerateField"));
56 return _autoGenerateField.Value;
58 set { _autoGenerateField = value; }
61 bool? _autoGenerateFilter;
62 public bool AutoGenerateFilter {
64 if (_autoGenerateFilter == null) {
65 throw new InvalidOperationException (string.Format (property_not_set_message, "AutoGenerateFilter"));
68 return _autoGenerateFilter.Value;
70 set { _autoGenerateFilter = value; }
77 throw new InvalidOperationException (string.Format (property_not_set_message, "Order"));
81 set { _order = value; }
84 private string GetLocalizedString (string propertyName, string key)
86 // If we don't have a resource or a key, go ahead and fall back on the key
87 if (ResourceType == null || key == null)
90 var property = ResourceType.GetProperty (key);
92 // Strings are only valid if they are public static strings
94 if (ResourceType.IsVisible && property != null && property.PropertyType == typeof(string)) {
95 var getter = property.GetGetMethod ();
97 // Gotta have a public static getter on the property
98 if (getter != null && getter.IsStatic && getter.IsPublic) {
103 // If it's not valid, go ahead and throw an InvalidOperationException
105 var message = string.Format (localization_failed_message, propertyName, ResourceType.ToString (), key);
106 throw new InvalidOperationException (message);
109 return (string)property.GetValue (null, null);
113 #region Consumer Methods
114 public bool? GetAutoGenerateField ()
116 return _autoGenerateField;
119 public bool? GetAutoGenerateFilter ()
121 return _autoGenerateFilter;
124 public int? GetOrder ()
129 public string GetName ()
131 return GetLocalizedString ("Name", Name);
134 public string GetShortName ()
136 // Short name falls back on Name if the short name isn't set
137 return GetLocalizedString ("ShortName", ShortName) ?? GetName ();
140 public string GetDescription ()
142 return GetLocalizedString ("Description", Description);
145 public string GetPrompt ()
147 return GetLocalizedString ("Prompt", Prompt);
150 public string GetGroupName ()
152 return GetLocalizedString ("GroupName", GroupName);