views:

2296

answers:

4

Hi, I am fairly new to reflection and I would like to know, if possible, how to create an instance of a class then add properties to the class, set those properties, then read them later. I don't have any code as i don't even know how to start going about this. C# or VB is fine.

Thank You

EDIT: (to elaborate)

My system has a dynamic form creator. one of my associates requires that the form data be accessible via web service. My idea was to create a class (based on the dynamic form) add properties to the class (based on the forms fields) set those properties (based on the values input for those fields) then return the class in the web service.

additionally, the web service will be able to set the properties in the class and eventually commit those changes to the db.

+4  A: 

If you mean dynamically create a class, then the two options are:

If you mean create an instance of an existing class, then start with Activator.CreateInstance to create an instance of the object, and then look at the methods on Type such as GetProperty which will return a PropertyInfo that you can call GetValue and SetValue on.


Update: For the scenario you describe, returning dynamic data from a web service, then I'd recommend against this approach as it's hard for you to code, and hard for statically-typed languages to consume. Instead, as suggested in the comments and one of the other answers, some sort of dictionary would likely be a better option.

(Note that when I say return some sort of dictionary, I am speaking figuratively rather than literally, i.e. return something which is conceptually the same as a dictionary such as a list of key-value pairs. I wouldn't recommend directly returning one (even if you're using WCF which does support this) because it's typically better to have full control over the XML you return.)

Greg Beech
The performance difference is an upfront cost, once all is said and done the code generated by Emit or CodeDom should be simillar in performance.
JoshBerke
@Josh - Good point, updated the answer to reflect this.
Greg Beech
you aren't allowed to return a dictionary in the web service
Russ Bradberry
additionally, if you use an array then you get something like: <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://blah.org/"> <string>blajsd</string> <string>f8js</string> <string>Sdft43tg</string> </ArrayOfString>
Russ Bradberry
@Russ - I was speaking figuratively rather than literally - i.e. return something which conceptually represents a dictionary such as a sequence of key-value pairs.
Greg Beech
+1  A: 

The Execution-Time Code Generation chapter of Eric Gunnerson's book (A Programmer's Introduction to C#) has some great information on this topic. See page 14 and onwards in particular. He outlines the two main methods of accomplishing dynamic class/code generation (CodeDOM and the Reflection.Emit namespace). It also discusses the difficulty and performance of the two approaches. Have a read through that, and you ought to find everything you might need.

Noldorin
A: 

The real question is, what do you need to use those properties for?

What are gonna be the use cases? Do you need to bind those properties to the UI somehow? Using what kind of technology? (WPF, Windows Forms?)

Is it just that you need to gather a set of key/value pairs at runtime? Then maybe a simple dictionary would do the trick.

Please elaborate if you can on what it is you need, and I'm sure people here can come up with plenty of ways to help you, but it's difficult to give a good answer without more context.

Denis Troller
+1  A: 

I know this is being overly simplified by why not just KISS and generate the required Xml to return through the Web Service and then parse the returned Xml to populate the database.

My reasoning is that for the expanded reason you suggest doing this I can see the value or reason for wanting a dynamic class?

Diago
it seems i was vastly overcomplicating things. thanks
Russ Bradberry