create properties at runtime?

J

joe

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
 
C

Cowboy \(Gregory A. Beamer\)

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

*************************************************
Think Outside the Box!
*************************************************
 
B

Ben Voigt

Cowboy (Gregory A. Beamer) said:
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.

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.
 
C

Cowboy \(Gregory A. Beamer\)

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.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top