Addressing Class Properties

G

Geo

Hi All,

I have a "huge" class with many attributes and need to change the value of
some them. The attribute with the value that has to be changed is read from
a xml file, i.e.:

class SalesOrder {
... private variables...

public string Id {
get { return _id; }
set { _id = value; }
}
public int Number {
get { return _number; }
set { _number = value; }
}
... and about 30 properties more...
}

the xml file is something like:
<SalesOrder>
<Modify field="Number" value="45" />
<Modify field="Date" value="06/25/2007" />
<Modify field="CustomerCode" value="MLT2384" />
... and so on...
</SalesOrder>

Finally, I defined an object and read the fields and values:

SalesOrder myOrder = new SalesOrder();
....
XmlReader reader = XmlReader.Create("settings.xml", settings);
....
fieldName = reader.GetAttribute("field");
fieldValue = reader.GetAttribute("value");
....

Now, I'd like to avoid the following code, where I have to check for every
attribute:

if (fieldName == "Number")
myOrder.Number = fieldValue;
else if (fieldName == "CustomerCode")
myOrder.CustomerCode = fieldValue;

Is there any way to do this more efficiently? Any code like:

myOrder.Set(fieldName, fieldValue);
or...
myOrder[fieldName] = fieldValue;


Thanks in Advance !!!
 
N

Nicholas Paldino [.NET/C# MVP]

Geo,

You will want to look into Reflection. With it you can use a string to
get a PropertyInfo instance for the class, and then call SetValue to set the
value of the property (you will have to type it correctly).

You would get the PropertyInfo instance from the Type instance (which
you can get from the instance by calling GetType on the instance).
 
G

G Himangi

If you use a 'switch' statement for strings instead of 'if', then I read
somewhere that C# sets up a HashTable to do the comparison, so it should be
pretty fast.

---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
 
M

Marc Gravell

Yes, the C# copmpiler would treat this as a hashtable lookup -
however, if speed is a concern (i.e. you are doing lots of such
access), then another (more versatile / less prone to missing
properties) aproach might be to look here:

http://www.codeproject.com/csharp/HyperPropertyDescriptor.asp

For optimal performance, cache the PropertyDescriptorCollection
(perhaps static), and re-use, i.e.

props[propertyName].SetValue(obj,value);

rather than (slower):

TypeDescriptor.GetProperties(obj)[propertyName].SetValue(obj,value);

Using this, the code is pretty-much as fast as compiled access (since
that is what it now is, just with a thin layer of indirection).

Marc
 

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