C
Chris R. Timmons
Since you asked...
I could use some help with CodeDOM. Here is how I am generating
these loops:
//
//TODO: generate this entire expression using CodeDOM classes:
StringBuilder formulaString = new StringBuilder();
formulaString.Append("dataPoints[row] = 0");
if (columns != null)
{
formulaString.Capacity = 40 * (columns.Count + 2);
for (int j = 0; j < columns.Count; ++j)
{
if ( ((MyColumnType)columns[j]).Role == RoleType.Independent)
{
formulaString.Append(System.Environment.NewLine);
formulaString.Append("+ coefficients[" + j + "] * table[row][" +
j + "] "); }
}
}
formulaString.Append(";");
//
The rest of the stuff (namespace, class, outer for loop, return
statement, etc.) is all generated purely without code snippet
expressions (with 1 exception -- see bug below). Anyone want to
show me how to do the above expression without code snippets?
(Note the need to break the lines to keep them below the 2046
char limit.)
The one other issue I am having trouble with is this:
compute.ReturnType = new CodeTypeReference("public float[]");
//suggested workaround for bug below
compute.Attributes |= MemberAttributes.Public;//has a bug
Is there an easy, language neutral way to work around the
MemberAttributes.Public bug when I return an array?
Dave,
[I don't mean to come across as arrogant in the post below. This is
just my long-winded way of saying that there may be a much simpler
solution to your problem.]
I've read most of the messages in the threads that deal with the
problems you're runing into. I'm not going to pretend that I know
what all of your requirements are, or the reason why you're writing
this code (learning exercise or production code?). But I get the
feeling that you may be trying to pound a round peg through a square
hole.
Lately I've become a big fan of Occam's Razor
(http://pespmc1.vub.ac.be/OCCAMRAZ.html). Dynamically generating and
compiling code is usually the slowest and most complex way to
implement a solution. It's almost always my last choice. When
designing a solution to a complex and/or large problem, my solution
generally comes from a list like this (in order of simplicity):
1. structured (or non-polymorphic OOP code) code w/ a fixed data
structure
2. same code as #1, but w/ a dynamic data structure
3. polymorphic OOP and/or interfaced code w/ a fixed data structure
4. same code as #3, but w/ a dynamic data structure
5. database solution (SQL)
6. potentially very complex code and data (writing a custom
compiler, lengthy regular expressions, using CodeDOM, etc.)
Another way of looking at this list:
1. simple code and data.
2. simple code and moderately complex data.
3. moderately complex code and simple data.
4. moderately complex code and data.
5. range of simple-to-complex code and data, but has an external
dependency on a database engine. May also have performance
drawbacks.
6. very complex code and data.
Too often I've learned (the hard way) that a complex solution to a
seemingly simple problem is often an indicator of a design flaw.
Using lists like this has helped me catch design errors like that and
fix them before they infect the rest of the app.
Just my $0.02...
Chris.