object instance property value

  • Thread starter Andrzej Kaczmarczyk
  • Start date
A

Andrzej Kaczmarczyk

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
A

Andrzej Kaczmarczyk

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
CUIN Kaczy
 

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