views:

72

answers:

3

I have a reporting module in an ASP.NET webforms app (C#) that uses dynamic controls for the parameters for each report.

These dynamic controls are built from an XML column in a SQL Server 2008 DB.

XML structure:

<Report Parameters>
    <Parameter>
        <Name>CustomerId</Name>
        <Control />
    </Parameter>
     <Parameter>
        <Name>Start Date</Name>
        <Control>DDL</Control>
    </Parameter>
</Report Parameters>

I've left out a lot of the elements for readability.

If the <Control> element is not empty (as in the case of the Start Date parameter in the XML example) then a C# based XSLT tranformation creates the appropriate control on my form .

If the <Control> element is empty (as in the case of the CustomerId) then I want to use an existing c# property with the same name (i.e. I have a CustomerId defined in my c# code).

These parameter values (CustomerId and Start Date) are then passed to a stored procedure used to generate the report data.

I use the XPathNavigator and associated classes in my C# code to yank out the <Name> element of any <Control> element that is empty.

The problem is that the <Name> element is in the form of a string and I really want it to be in C# code form (sorry can't think of a better way to describe this!) i.e. I want this.CustomerId (added the this to show it's code not a string) not "CustomerId".

As I don't know how many reports this system will have over time I don't really want to build up an enum or switch statement if "CustomerId" then this.CustomerId etc. as it seems to negate the point of dynamic code.

I realise that reflection will probably be needed and I have little knowledge of this so any advice or advice on a different approach to this problem would be welcome.

Thanks,

Rich.

A: 

You want to dynamically generate a class and it's properties based on the xml contents? And you want it without having to generate code and recompile i'm sure too. I have tried that too once and i never managed to get it working.

I don't see any solution, sorry.

Jeroen
A: 

What you are looking for is a Code generator. While I haven't implemented one myself, I have used templates created by others before and it works like a charm. You can get started here.

Chandam
A: 

This should do it:

this.GetType().GetProperty("CustomerId").SetValue("YourValue");
highphilosopher
I think it's not what he wants. He wants it hardcoded, with intellisense.
Jeroen