DisplayName attribute

Definicja atrybutu, pozwalającego na nadanie specyficznej nazwy właściwościom klasy. Jeżeli podamy resourceKey i resourceType, wówczas nazwa będzie pobierana ze zdefiniowanych resoursów.

Atrybut ten posłużył mi, podczas przygotowywania biblioteki do eksportu danych do formatu CSV na podstawie definicji klasy.
Dzięki temu atrybutowi w pliku wyjściowym generowane są nagłówki w odpowiednim języku.

    /// 
    /// Specifies the display name for a propery.
    /// 
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class DisplayNameAttribute : Attribute
    {
        private string _displayName;
        /// 
        /// Gets a display name. If ResourceKey and ResourceType are provided then this value is taken from resources.
        /// 
        public string DisplayName
        {
            get
            {
                SetDisplayNameByResourceKey();
                return _displayName;
            }
        }

        private string ResourceKey { get; set; }

        private Type ResourceType { get; set; }

        /// 
        /// Initializes a new instance of the DisplayNameAttribute class.
        /// 
        /// The display name.
        public DisplayNameAttribute(string displayName)
        {
            _displayName = displayName;
        }

        /// 
        /// Initializes a new instance of the DisplayNameAttribute class.
        /// 
        /// The resource key indicates display name in resources.
        /// The resource type where resource key is included.
        public DisplayNameAttribute(string resourceKey, Type resourceType)
        {
            ResourceKey = resourceKey;

            ResourceType = resourceType;
        }

        private void SetDisplayNameByResourceKey()
        {
            if (ResourceType == null || ResourceKey == null)
                return;

            PropertyInfo property = ResourceType.GetProperty(ResourceKey, BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);

            if (property == null)
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DisplayNameResources.DisplayNameAttribute_ResourceTypeDoesNotHaveProperty,
                                                                  ResourceType.FullName, ResourceKey));
            if (property.PropertyType != typeof(string))
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DisplayNameResources.DisplayNameAttribute_ResourcePropertyNotStringType,
                                                        property.Name, ResourceType.FullName));

            _displayName = (string)property.GetValue(null, null);
        }
    }

Poniżej użycie na przykładzie klasy User

public class User
    {
        public int Id { get; set; }
       
        [DisplayName("FirstName", typeof(Resources.DisplayName))]
        public string FirstName { get; set; }

        [DisplayName("Last Name")]
        public string LastName { get; set; }

        [DisplayName("DateOfBirth", typeof(Resources.DisplayName))]
        public DateTime? DateOfBirth { get; set; }        

        public string Email { get; set; }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s