1

Is it possible to use an autogenerated class in the same run as it is being generated? I am trying to achieve something which works as a factory but for autogenerated classes. My scenario is like this:

I have files which specify what some resulting files can contain. These result files should then be used to calculate statistics or something other than that. Now I would like to modify the data when these are read but since the software does not know what parts are written to file, it does not know how to save the data in the files internally. I have a reader and writer (made by a friend and which are VERY genreric and too cumbersome to use). I figured that I could use the definitions to autogenerate classes/structs.

for instance: classes which can be written are "A", "B" and "C". Say that "A" and "B" have two and three children respectively. Lets call these A0, A1, B0, B1,B2.

Now internally in the software, I would like to write:

A aObject = new A0();
aObject.variable1 = 2;
aObject.variable2 = "something";

B bObject = new B1();
bObject.SomeVariable = 42;
bObject.SomeOtherVariable = "someValue";

what the variables are called and what type the data represents is written in a specification file. In xml for instance, it would be something like:

<A0>
<Variable>
   <Name>variable1</Name>
   <type>int</type>
</Variable>
<Variable>
   <Name>variable2 </Name>
   <type>string</type>
</Variable>
</A0>

(same with A1, B0, B1, B2 and so on)

Now, I would like to read a result file, realize I would have to autogenerate A0, generate it, modify the data (with the class/struct A0 which was just created) and then write to file again.

My solution for this is to pregenerate all possible classes/structs beforehand and include them. Problem with this is that the versions of A, B and C are or will be changing.

All help is appreciated :)

Juicef
  • 196
  • 3
  • 11
  • oh right, in pseydocode it looks like this: Generator.generate(A0,A0specification); include to project (somehow); A0.somevariable = "something"; I know this will imply compiling errors since A0 does not exist at compile time. Just asking ;D – Juicef Aug 30 '11 at 09:53

1 Answers1

1

The rest of your question did not make all that much sense to me to be honest, but to answer your very first question: yes you can do that. Have a look here for instance.

Regards Gert-Jan

Community
  • 1
  • 1
gjvdkamp
  • 9,929
  • 3
  • 38
  • 46
  • This was EXACTLY what I was looking for hehe. Sorry for my weirdly defined question. Thank you! :D – Juicef Aug 30 '11 at 10:40
  • Also, a tip from me. That thread is very pro CodeDom. In my personal experience I found it much easier generating C# code from templates and compiling that. If you do it like that you can just write code as you would normally, debug that and then chop it up into templates it to use it in a code generator. Much less abstract thn the other approaches. Just my 2 cents, good luck! – gjvdkamp Aug 30 '11 at 11:14