0

I'm representing the contents of a (rigidly defined) Excel sheet in a WPF application. An "ExcelContainer" class parses the .xlsx and slots all data into matching ExcelContainer fields. Each field has a public property for two-way data-binding, and the the WPF client exposes all data to the user this way.

I'm manually creating & keeping up-to-date all ExcelContainer fields and properties, and there's a lot of them. Maintenance is becoming problematic as the input .xlsx grows... it's not pretty or efficient. I'm looking for a strategy to automate all of this.

One idea is to auto-generate the getters and setters at runtime, or emit an auto-generated code file with all ExcelContainer properties. I'm looking for C#-specific approaches to do this (in VS2015, if that matters). I can imagine brute-force code file generation via a build system, but I suspect that C# gives me better opportunities than that.

Links to blog posts etc. welcome!

Drakestar
  • 698
  • 10
  • 27
  • 1
    There are many ways to skin that cat. 1. T4 Templates for CodeGen, 2. Use PropertyDescriptors to generate fake properties at runtime 3. ExpandoObject – hoodaticus Feb 13 '17 at 20:32
  • 1
    Another workaround is to use 'DataTable' and add columns with mentioned properties there. You can then use this DataTable for binding purposes. – AjS Feb 14 '17 at 08:58
  • 1
    You can't really add real properties to an object at runtime but you could use a an ObservableDictionary where each key represents a "property": http://stackoverflow.com/questions/4964683/notify-property-changed-on-a-dictionary. – mm8 Feb 14 '17 at 11:04
  • Thanks everybody. I'd like to keep things in the runtime. ExpandoObject and ObservableDictionary sound like my initial best bets, since both already implement IPropertyChanged. I'll start with looking into subclassing ExpandoObje... (nm, just as I type this I see that it's sealed)... *using* ExpandoObject and see if I can wrap it so that all of my code gets executed as the Properties are accessed. Will update the question with settled approach as I finish this. – Drakestar Feb 14 '17 at 17:27

0 Answers0