|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
create properties at runtime?Hi,
I want to create some sort of engine, that would read some keys from an xml document and for each key to create a property for the engine; Because I want to use one single class with several xml files, I thought of making strong typed properties (or at least string properties) at run-time. Any ideas? Thank you, Dan If you truly want to take a class, or better yet, extend a class at runtime,
you have a couple of options. First is reflection.emit, which allows you to create and compile a class, in memory, on the fly. You can also dig into the Code DOM and do the same thing. I am not sure, however, that creating a class to act as a property bag is the best option. Simply holding keys in memory, as a DataSet for example, will serve the same purpose without the infrastructure needed to support "on demand" classes. Without knowing more about your project, I endorse neither approach, but, as classes are the more time consuming and complex route, consider simpler methods before heading that route. -- Show quoteGregory A. Beamer ************************************************* Think Outside the Box! ************************************************* "joe" <j**@gmail.com> wrote in message news:Oy%2392lxfGHA.2456@TK2MSFTNGP04.phx.gbl... > Hi, > > I want to create some sort of engine, that would read some keys from an > xml document > and for each key to create a property for the engine; > > Because I want to use one single class with several xml files, I thought > of making strong typed properties > (or at least string properties) at run-time. > Any ideas? > > Thank you, > Dan >
Show quote
"Cowboy (Gregory A. Beamer)" <NoSpamMgbworld@comcast.netNoSpamM> wrote in I definitely agree with Greg. I've recently gone the Reflection.Emit route, message news:%23bMansyfGHA.4004@TK2MSFTNGP04.phx.gbl... > If you truly want to take a class, or better yet, extend a class at > runtime, you have a couple of options. First is reflection.emit, which > allows you to create and compile a class, in memory, on the fly. You can > also dig into the Code DOM and do the same thing. > > I am not sure, however, that creating a class to act as a property bag is > the best option. Simply holding keys in memory, as a DataSet for example, > will serve the same purpose without the infrastructure needed to support > "on demand" classes. Without knowing more about your project, I endorse > neither approach, but, as classes are the more time consuming and complex > route, consider simpler methods before heading that route. > > -- > Gregory A. Beamer because I was creating 300 classes from XML files which will now form the object model for additional coding. How will these dynamic properties be accessed? No code compiled before reading the XML file can possibly use the new properties. Only if you have some sort of scripting language (JScript.NET eval for example) would this be helpful, but JScript already supports extender properties. For storage and enumeration, a Dictionary is the simplest way to go. If you are adding properties to scriptable objects, look into the way JScript.NET and/or IronPython do it. Show quote > > ************************************************* > Think Outside the Box! > ************************************************* > "joe" <j**@gmail.com> wrote in message > news:Oy%2392lxfGHA.2456@TK2MSFTNGP04.phx.gbl... >> Hi, >> >> I want to create some sort of engine, that would read some keys from an >> xml document >> and for each key to create a property for the engine; >> >> Because I want to use one single class with several xml files, I thought >> of making strong typed properties >> (or at least string properties) at run-time. >> Any ideas? >> >> Thank you, >> Dan >> > > You end up having to continue to reflect to late bind. If you are using
Visual Basic, you can use the late binding syntax, which makes this a bit easier; for C#, you are stuck with Reflection to late bind. There is a risk of a miss when you late bind, however, as a failed compilation will not be found until you attempt to access. This is when a good "try" strategy is in order. Do you catch and attempt to create and then run, or do you simply use finally to clean up and allow errors up the stack? Eval() would be nice, as the language would have a quick way of testing. -- Show quoteGregory A. Beamer ************************************************* Think Outside the Box! ************************************************* "Ben Voigt" <rbv@nospam.nospam> wrote in message news:uFAytk4fGHA.324@TK2MSFTNGP02.phx.gbl... > "Cowboy (Gregory A. Beamer)" <NoSpamMgbworld@comcast.netNoSpamM> wrote in > message news:%23bMansyfGHA.4004@TK2MSFTNGP04.phx.gbl... >> If you truly want to take a class, or better yet, extend a class at >> runtime, you have a couple of options. First is reflection.emit, which >> allows you to create and compile a class, in memory, on the fly. You can >> also dig into the Code DOM and do the same thing. >> >> I am not sure, however, that creating a class to act as a property bag is >> the best option. Simply holding keys in memory, as a DataSet for example, >> will serve the same purpose without the infrastructure needed to support >> "on demand" classes. Without knowing more about your project, I endorse >> neither approach, but, as classes are the more time consuming and complex >> route, consider simpler methods before heading that route. >> >> -- >> Gregory A. Beamer > > I definitely agree with Greg. I've recently gone the Reflection.Emit > route, because I was creating 300 classes from XML files which will now > form the object model for additional coding. How will these dynamic > properties be accessed? No code compiled before reading the XML file can > possibly use the new properties. Only if you have some sort of scripting > language (JScript.NET eval for example) would this be helpful, but JScript > already supports extender properties. > > For storage and enumeration, a Dictionary is the simplest way to go. > > If you are adding properties to scriptable objects, look into the way > JScript.NET and/or IronPython do it. > >> >> ************************************************* >> Think Outside the Box! >> ************************************************* >> "joe" <j**@gmail.com> wrote in message >> news:Oy%2392lxfGHA.2456@TK2MSFTNGP04.phx.gbl... >>> Hi, >>> >>> I want to create some sort of engine, that would read some keys from an >>> xml document >>> and for each key to create a property for the engine; >>> >>> Because I want to use one single class with several xml files, I thought >>> of making strong typed properties >>> (or at least string properties) at run-time. >>> Any ideas? >>> >>> Thank you, >>> Dan >>> >> >> > > |
|||||||||||||||||||||||