home page blog programming articles

Using reflection and XML to create a dynamic UI

Sample Code

The sample code for this article is here

Apologies

I was going to write a much bigger writeup on this but I never seem to get around to it and since christmas is now over I'll just have to admit defeat on this one. The code is not production quality and should only really be used as a starting point for your own version. - Peter Ibbotson 12 Jan 2005

Installing and running the sample

I've checked this works with the november CTP and it should still work with the august(ish) releases of VS.NET 2005. It's probably possible to back port to an earlier version.

The easiest thing to do is to unzip the file into the root of your C drive thus creating a directory called c:\dnug (If you want to change this, there is a setting in the app.config in the DNUGDemo project called BaseDirectory which should allow a different directory name)

Make sure c:\dnug\SessionData.xml is read write and your unzipper didn't create it readonly
Open the solution in c:\dnug\dnguidemo\
Build and run it
Login, there are only two valid users "Peter" and "Mark" (This is case sensitive) who both have different styles of form, open up the forms to get a feel for what their setups are.

Basic structure notes

There are a lot of projects in here. They broadly speaking split into four camps

Core.dll - A subsitute business layer that uses generics to get a quick and dirty database (aka an XML file) loaded into memory

MaintCustomers.dll,MaintProducts.dll,MaintSuppliers.dll - these are the "maintain" programs and provide properties decorated with attributes that the main executable can use to display on screen. They also implement IBrowsable which handles moving around the business layer objects

PrintCustomers.dll,PrintProducts.dll,PrintSuppliers.dll - these are the "reporting" programs which implement a simple report interface IReport which returns a List of lines to display on screen by the main executable

UIAttributes.dll - Contains the definitions for interfaces and the attributes used to decorate the properties and methods in the "program" DLLs

DNUGDemo.exe - The meat of the program responsible for loading in the XML, creating the forms and binding the fields and methods to menu entries

Finally the XML for each "program" lives in c:\dnug\Peter or c:\dnug\Mark depending upon who you've logged in as

Hopefully that should get you started as I said I wanted to do something better, but I never seem to get time and after four months I don't think I ever will - PeterI Jan 2005