Reflection to get a public property

E

Eric Sabine

This is for a web app but I figured it's more of a lanugage Q:

I have a user control which is going to be used by multiple web pages.
In the web control I need to get the bool value of a public property.
All of the web pages implement an interface with this property.

In the user control, I was going with Reflection to learn this value
but I need help with the code

PropertyInfo pi =
this.Page.GetType().GetProperty("IncludeMerchantOwned");

if (pi != null)
{
object o = null;
// the line below is my problem
m_includeMerchantOwned = (bool)pi.GetValue(o, null);
}

I'm sure it's the object o. This is where I need help.

Eric
 
T

Tom Porterfield

I have a user control which is going to be used by multiple web pages.
In the web control I need to get the bool value of a public property.
All of the web pages implement an interface with this property.

In the user control, I was going with Reflection to learn this value

If you've created an interface, and all of your pages implement the
interface, then there is no need to use reflection to get the value. Just
read the property from the page within your user control. Ex:

IInterfaceWithIncludeMerchantOwned iI = this.Page as
IInterfaceWithIncludeMerchantOwned;
if (iI != null)
{
m_includeMerchantOwned = iI.IncludeMerchantOwned;
}
 
B

Bruce Wood

Tom's suggestion is the superior alternative, but if for some reason
you can't go with what he wrote, here is the problem with the code you
submitted:

object o = null;
// the line below is my problem
m_includeMerchantOwned = (bool)pi.GetValue(o, null);

The first argument to GetValue() is the object from which to fetch the
property value. To understand why this is so, you need to understand
what a PropertyInfo object contains. PropertyInfo does _not_ contain a
reference to the original this.Page that you used to get the property
info in the first place. All PropertyInfo does is contain information
about a particular property of a particular _class_, or, if you wish,
_any instance of a particular class_. This has to be so, if you think
about it, because you get PropertyInfo from a Type, not directly from
your object.

So, when you say GetValue, you have to say, "from where"? Or, more
precisely, "from what object"? The PropertyInfo knows about the class
and the property, but not about any object instance. You have to give
it that.

In your code, you don't even instantiate an object; you just create a
reference to an Object and make that reference null. Not only are you
passing no instance into GetValue (null), to add insult to injury,
you're passing in a reference of type Object, which doesn't implement
your interface and doesn't have the IncludeMerchantOwned property. No
wonder poor GetValue is unhappy!

You need to say this, instead:

// the line below is my problem
m_includeMerchantOwned = (bool)pi.GetValue(this.Page, null);

Now you're telling GetValue that you want to get the property value
from the object instance pointed to by the this.Page property, which
should work much better. :)
 
E

Eric Sabine

Outstanding. Thank you Tom & Bruce for both posts. Both were well
detailed and well thought out.
Eric
 

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