.net - c# choosing class properties with a variable? -


I am adding functionality to one of my existing (but broken) systems, catching XML file from a web service Takes it, parses it and then does some stuff before packing it back into our database.

The previous developer (which has been left now) has left me this little gem:

And I wonder if this is how a is the way?

Can I loop through each node and assign wo object to its name?

> Some such (pseudo-code):

foreach in XmlNode xn WorkorderNodeTree {// Name of the property of the current node address / // property in the workorder class Match // set values ​​equal wo & Lt; Xn.name & gt; = Xn.innertext}

Now the only thing I get that gets (from the interweb):

  foreach (in XmlNode xl myXML) { Object o = assembly Gate Acceding Assembly () CreateInstine ("Worker", True); Type t = xl.Name.GetType (); Property Info PI = T. GetProperty (XL., Nom); Pi.SetValue (O, XL.Intext, blank); }  

But it gives an empty reference exception on o. I'm a little confused, any suggestions?

I anticipate doing this, I need to use reflection or generic, but I have never hit these things - can someone advise me the right direction or at least reflection Try to interpret?

Many thanks all, sorry for the horrific long post!

Edit:

Thank you, very deep and sincere thanks to Frederick and Rites - both of you are white knights in my DAB office environment. Rytmis' code has resolved the issue of editing, but I have learned so much in this hour or so - thanks guys, really appreciate it.

I think your code may need some adjustments

  foreach (xmlnode xl in myxml) {object o = assembly Gate Excepting Assembly () CreateInstance ("Worker", True); Type t = xl.Name.GetType (); Property Info PI = T. GetProperty (XL., Nom); Pi.SetValue (O, XL.Intext, blank); }  

This creates a new example of work order for each property you are setting up, and also tries to reflect property infosy by name. Gettype () which is actually typing, and not typeoff (work order) like you want it to be. Instead:

  workorder w = new workorder (); Type t = typing (work order); Forex Currency (XML XML XML in my XML) {PropertyInfoPI = T. GreatProperty (XL.); Pi.SetValue (w, xl.InteText, null); }  

[edit] You may also want to specify some compelling flags:

  PropertyInfo pi = t.GetProperty (xl.Name, BindingFlags. Instance | bindingsflags.public | binding flags. Unknown cass);  

This may or may not be possible I can never remember what the default is :)


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -