Iterate Over Object Properties and Property Attributes w/Reflection

Define custom attribute class as follows as well as placing the attribute reference on the property “FirstName”.  In the following we are creating our own custom attribute by defining an attribute class which derives from Attribute which makes identifying attribute definitions in metadata easy.  The AttributeUsage attribute can be used to limit which asset the attribute can be placed such as class, struct, property etc.  In addition, in the example below I have disallowed multiple similar attributes from being used on the same property designated by AllowMultiple = false


    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
    public class CustomItemAttribute : Attribute {

        public string FieldName { get; set; }

        private bool _isMultiValue = false;
        public bool IsMultiValue {
            get { return _isMultiValue; }
            set {
                _isMultiValue = value;

        public CustomItemAttribute(string fieldName) {
            this.FieldName = fieldName;        

   public class Item{
        public string FirstName { get; set;}


Now to iterate over the properties. Notice in the class Item above how we can exclude the 'Attribute' text in the name of the attribute applied to the FirstName property. Below we can use Type.GetProperties method to get the names of the properties for a specific type. The method GetProperties returns an array of PropertyInfo objects and the property names aer available through PropertyInfo.Name. If you want to get only a subset of the properties such as public static ones you can use BindingFlags parameters (Public/NonPublic, Instance/Static). i.e. PropertyInfo[] infos = typeof(Item).GetProperties(BindingFlags.Public|BindingFlags.Static);

        private void IterateOverProperties() {
            CustomItemAttribute customItemAttribute;
            Type type = typeof(Item);
	    //for each property of object of Item
            foreach (PropertyInfo propInfo in type.GetProperties()) {
                //for each custom attribute on the property loop
                foreach CustomItemAttribute attr in propInfo.GetCustomAttributes(typeof(CustomItemAttribute), false)) {
                    customItemAttribute = attr as CustomItemAttribute;
                    if (customItemAttribute != null) {
                        string propertyName = propInfo.Name;
                        string fieldName = customItemAttribute.FieldName;			
			//TODO: add your logic here



comments powered by Disqus


Recent Tweets

Maybe resolved itself, after resetting emulator and couple restarts - will let you know
@MillerMark A few weeks ago you tweeted that something huge/incredible was coming....what is/was it? (I am hoping s……
Oddly, it won't hit breakpoints in XF .net standard 2 project now either
@JamesMontemagno Updated to 15.7 preview 2 Visual Studio - now having lots of odd thread issues when starting debug……