jeudi 15 novembre 2018

C# NET Looping over object properties stored in List

At the moment I'm working on funcionality that involves exporting and importing data to Xlsx file. Here's what I want to do: I want to have an attribute I can put above a property like this.

public class MyClass
{
   [XlsxColumn("Column 1")]
   public string myProperty1 { get; set; }
   public int myProperty2 { get; set; }
}

So far I don't have problems, but then I want to "store references" to properties marked with the XlsxColumn attribute. I'm using reflection to store properties data in List

var propsList = MyClass.GetProperties().Where(
   prop => Attribute.IsDefined(prop, typeof(XlsxColumn)));

I have a list with all properties marked with XlsxColumn (only myProperty1 in this example). Now I want to loop over properties in MyClass, but only properties with that attribute (so all PropertyInfo objects stored in propsList variable), without resorting to reflection with each object saved to Xlsx file.

I'm restricted to .NET 4.0.

Thanks for your time.





Aucun commentaire:

Enregistrer un commentaire