Get all attribute properties with values for a specific attribute type - CSharp System

CSharp examples for System:Attribute

Description

Get all attribute properties with values for a specific attribute type

Demo Code


using System.Collections.Generic;
using System;//www  . ja v  a2 s.c om

public class Main{
        /// <summary>
        /// Get all attribute properties with values for a specific attribute type
        /// </summary>
        /// <param name="attribute">attribute to check against</param>
        /// <returns>collection of all properties with values</returns>
        public static IDictionary<string, object> GetAttributeProperties(Attribute attribute)
        {
            Type attributeType = attribute.GetType();
            IDictionary<string, object> attributes = new Dictionary<string, object>();
            foreach(var property in attributeType.GetProperties())
            {
                object value = property.GetValue(attribute, null);
                attributes.Add(property.Name, value);
            }
            return attributes;
        }
}

Related Tutorials