Groups | Blog | Home
all groups > c# > june 2007 >

c# : Addressing Class Properties


Geo
6/25/2007 11:07:46 PM
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 !!!
Nicholas Paldino [.NET/C# MVP]
6/25/2007 11:11:08 PM
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).


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

[quoted text, click to view]
G Himangi
6/26/2007 12:00:00 AM
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
---------



[quoted text, click to view]

Marc Gravell
6/26/2007 12:00:00 AM
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

Robbe Morris - MVP C#
6/28/2007 10:48:12 PM
You could use a variant of this article and Reflection.
You could analyze the node and then look through
the PropertyInfo[] of the class.

http://www.eggheadcafe.com/tutorials/aspnet/a4264125-fcb0-4757-9d78-ff541dfbcb56/net-reflection--copy-cl.aspx

--
Robbe Morris
EggHeadCafe.com
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp



[quoted text, click to view]
AddThis Social Bookmark Button