I have an interesting problem.
I am developping an ASP.net application, but the question is more related to architecture.
The thing is, I need to present summary information per year. It's not the exact case, but imagine that I need to present total sales by product and year, with one column for the product name and one for each year.
So something like
Product 1998 1999 2000
prod. A 1,000 1,500 1,800
My problem is, I don't know how many years I need to report, it depends on what's on the database.
I am using collections, no datasets as datasource.
Each object in the collection will have 3 properties relevant to this report.
string ProductName;
int SalesYear;
double Sales;
so of course I couls iterate through the entire collection and dynamically build a table, but I was hoping for a more elegant solution.
I thought, what if I can have an object that will contain all the summary information for each product.
something like this:
string ProductName
double Sales1998;
double Sales1999;
and so on.
that would make it very easy to bind to a GridView for example. I would still have to add the Gridview columns at run time but it would be just a few lines of code.
But how do I achieve that
One way is through Reflection.Emit.
Basically, I need to determine the range of years (not a problem).
I also would have an abstract class with some common properties, such as the product name.
Then I would dynamically create the object, using the abstract class as the base class and adding as many properties as years.
The questions are:
1) Is this an efficient way to do it is there a better way
2) How can I create a List<T> where T is my dynamically created object
3) for each member of the collection (each object), do I have to repeat the whole process or can I just define the class once using Reflection.Emit and then create instances based on that definition
I know there is also a way to do this using CodeDom but I haven't found any article with details on how to do it.
Of course, this can be improved in many ways, like creating dynamic interfaces to allow different implementations but the basics are the the ones described above.
Please tell me what you think.
Has anybody tried something like this is the performance penalty too high
BTW, I am doing this on 2.0, as you can guess by the use of Generics.
Thanks in advance.

design Reflection.Emit question.
RuneFS
Did you looked at an objectdatasources with a datalist or repeator I think it's beter than Reflection.Emit. Or even easier to use is xml with xlst but I don't know if your datasources is xml.
vijayasekar
I use ObjectDataSource all the time.
A repeater or a datalist would not help, they actually would do it harder.
The problem is, to use DataBinding you have to bind to a property, you can't bind to a field or a method.
Since I don't know which properties I need at design time, I have to do it at runtime.
My options are basically two
1) Expose the collection of different years as a property and on the webform (or a helper class) iterate through each collection and fill the columns accordingly. Don't like it.
2) Dynamically add properties to my objects.
Number 2 can be achieved in 3 different ways, as far as I know.
1) Using Reflection.Emit, which implies basically get an instance of the Application Domain, create a ModuleBuilder, then a TypeBuilder and finally a PropertyBuilder.
2) Using CodeDom, there are methods that basically write source code for you.
3) Implementing the ITypedList on a collection and then use a TypeDescriptor to return the list of properties I want to expose. The problem with this is that those properties must actually exist in the object and to do that I need to create a class that implements IExtendedProvider.
I am not too familiar (that's an understatement) with System.CodeDom or System.ComponentModel that are basically not designed to alter a class but to provide compile time or design time support
Sillva
This is the right way to do it. There is absolutely nothing elegant about using Reflection.Emit.
Yoni
Some Ideas:
1.My experience with reflection in general is that it is detrimental to performance.
2.My experience with Reflection.Emit() : way to complex, not worth the effort or maintenance and testing effort.
What I would do:
1.Create a stored procedure that would do all the iterations and calculations (better for performance and masking complexity)
2. Retrieving the data already processed and calculated from a temp table, then binding the control to a datacollection.
3. Sales year should be a row not a column.
4. Displaying the data horizontally (if needed) could be easily done with xsl or using a cool component from a control suite.
I hope this helps.
Rudolfh Bantim
I have a similar issue - I want to databind a combobox to a List<DynamicType>, where DynamicType is the type I have created through Reflection.Emit.