Can I autogenerate/compile code on-the-fly, at runtime, based upon values (like key/value pairs) parsed out of a configuration file?
Posted
by
Kumba
on Stack Overflow
See other posts from Stack Overflow
or by Kumba
Published on 2011-01-02T06:12:54Z
Indexed on
2011/01/03
1:53 UTC
Read the original article
Hit count: 595
vb.net
This might be a doozy for some. I'm not sure if it's even 100% implementable, but I wanted to throw the idea out there to see if I'm really off of my rocker yet.
I have a set of classes that mimics enums (see my other questions for specific details/examples). For 90% of my project, I can compile everything in at design time. But the remaining 10% is going to need to be editable w/o re-compiling the project in VS 2010. This remaining 10% will be based on a templated version of my Enums class, but will generate code at runtime, based upon data values sourced in from external configuration files.
To keep this question small, see this SO question for an idea of what my Enums class looks like. The templated fields, per that question, will be the MaxEnums
Int32, Names
String() array, and Values
array, plus each shared implementation of the Enums
sub-class (which themselves, represent the Enums that I use elsewhere in my code).
I'd ideally like to parse values from a simple text file (INI-style) of key/value pairs:
[Section1]
Enum1=enum_one
Enum2=enum_two
Enum3=enum_three
So that the following code would be generated (and compiled) at runtime (comments/supporting code stripped to reduce question size):
Friend Shared ReadOnly MaxEnums As Int32 = 3
Private Shared ReadOnly _Names As String() = New String() _
{"enum_one", "enum_two", "enum_three"}
Friend Shared ReadOnly Enum1 As New Enums(_Names(0), 1)
Friend Shared ReadOnly Enum2 As New Enums(_Names(1), 2)
Friend Shared ReadOnly Enum3 As New Enums(_Names(2), 4)
Friend Shared ReadOnly Values As Enums() = New Enums() _
{Enum1, Enum2, Enum3}
I'm certain this would need to be generated in MSIL code, and I know from reading that the two components to look at are CodeDom
and Reflection.Emit
, but I was wondering if anyone had working examples (or pointers to working examples) versus really long articles. I'm a hands-on learner, so I have to have example code to play with.
Thanks!
© Stack Overflow or respective owner