473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

object instance property value

Hi, I have following problem:
(warning, quite long post with lots of code)

I have an object of a known type
like this.
class MyClass {
public int Property1;
public string Property2;
}

MyClass myClassObject;
object myObject = myClassObject;

of course I can do the reverse operation
MyClass myClassObject2 = (MyClass) myObject;

I have the string name of the property
string propertyName = "Property1";

I want to get the value from the object that is equivalent of:
object propertyValue = myClassObject.Property1

but using the string propertyName, and packaged or unpackaged object.

The only way I can think of is to do something as ugly as:

IGetAble {
object Get(string propertyName);
}

MyClass : IGetAble
{
public int Property1;
public string Property2;

object Get(string propertyName) {
object retValue;
switch (propertyName) {
case "Property1":
retValue = this.Property1;
break;
case "Property2":
retValue = this.Property2;
break;
default:
throw new ArgumentException("no such property");
}
return retValue;
}
}

and then
IGetAble instance = (IGetAble) myObject;
object propertyValue = instance.Get("Property1");

but this is UGLY!! it forces me to implement this method in each and every
class with this monstrous switch. The surely should be something in
reflection assembly that allow getting object instance property value by
type, name and instance.

Anyone know how to do it?

thanks
CUIN Kaczy
Nov 17 '05 #1
2 1860
Andrzej,

You are right, you can use reflection. Assuming you have the following:

// The object that has the instance.
object o = ...;

// The name of the field.
string field = "Property1";

You can do the following:

// Get the type.
Type type = o.GetType();

// Get the field info.
FieldInfo fieldInfo = type.GetField(field);

// Now get the value.
object val = fieldInfo.GetValue(o);

It should be noted that the above uses a FieldInfo instance because you
are exposing public fields, not properties. You really should be exposing
properties, not fields (it's actually an FxCop rule, as well as explicitly
stated as a "bad thing" in the design guidelines). If you did want a
property, you would change the call to GetField on type to GetProperty.
Also, if the property/field is private/protected, then you would have to
call teh overload of GetField/GetProperty which takes a combination of
values from the BindingFlags enumeration, indicating that you want non
public fields/properties as well.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Andrzej Kaczmarczyk" <ak**********@visualsystems.com.pl> wrote in message
news:uu**************@tk2msftngp13.phx.gbl...
Hi, I have following problem:
(warning, quite long post with lots of code)

I have an object of a known type
like this.
class MyClass {
public int Property1;
public string Property2;
}

MyClass myClassObject;
object myObject = myClassObject;

of course I can do the reverse operation
MyClass myClassObject2 = (MyClass) myObject;

I have the string name of the property
string propertyName = "Property1";

I want to get the value from the object that is equivalent of:
object propertyValue = myClassObject.Property1

but using the string propertyName, and packaged or unpackaged object.

The only way I can think of is to do something as ugly as:

IGetAble {
object Get(string propertyName);
}

MyClass : IGetAble
{
public int Property1;
public string Property2;

object Get(string propertyName) {
object retValue;
switch (propertyName) {
case "Property1":
retValue = this.Property1;
break;
case "Property2":
retValue = this.Property2;
break;
default:
throw new ArgumentException("no such property");
}
return retValue;
}
}

and then
IGetAble instance = (IGetAble) myObject;
object propertyValue = instance.Get("Property1");

but this is UGLY!! it forces me to implement this method in each and every
class with this monstrous switch. The surely should be something in
reflection assembly that allow getting object instance property value by
type, name and instance.

Anyone know how to do it?

thanks
CUIN Kaczy

Nov 17 '05 #2
> Andrzej,
Hi Nicholas

You are right, you can use reflection. Assuming you have the
following:

// The object that has the instance.
object o = ...;

// The name of the field.
string field = "Property1";

You can do the following:

// Get the type.
Type type = o.GetType();

// Get the field info.
FieldInfo fieldInfo = type.GetField(field);

// Now get the value.
object val = fieldInfo.GetValue(o);
Thanks, I am checking this right now ....
working :D

excelent, thank you very much, gotta read more about the reflection.
It should be noted that the above uses a FieldInfo instance because you
are exposing public fields, not properties. You really should be exposing
properties, not fields (it's actually an FxCop rule, as well as explicitly
stated as a "bad thing" in the design guidelines). I am using properties, the above was just for simplicity, I chcekced prior
to positing for some get value methods, but didn't get as far as to find the
get alue in propertyInfo :)

One clarification please, is a getProperty -> getValue a "bad thing" or this
only apply to the getField->getValue

If you did want a property, you would change the call to GetField on type to GetProperty.
Also, if the property/field is private/protected, then you would have to
call teh overload of GetField/GetProperty which takes a combination of
values from the BindingFlags enumeration, indicating that you want non
public fields/properties as well. I only want to get acces the public properties. however if I'd be needing
something else can you point me to some papers additional to what MSDN
provides?

Hope this helps. it did. thanks again

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

CUIN Kaczy

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
2
by: Chris Murphy via DotNetMonster.com | last post by:
Hey guys, I've been hitting a brick wall with a problem I've come accross in developing an application. Background: The application uses one primary class that I'm trying to implement with the...
3
by: | last post by:
Hi all, I have a question on reflection Lets say I have a custom object called Address. Now, lets say I have a string variable that holds the name of a variable in the object such as...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
28
by: Stef Mientki | last post by:
hello, I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs). My first action is to translate the JAL code into Python code. The reason for this approach is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.