469,270 Members | 1,011 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Reflection to get a public property

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("IncludeMerchantOw ned");

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

Nov 16 '05 #1
3 4244
On 2 Feb 2005 08:48:57 -0800, Eric Sabine wrote:
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;
}
--
Tom Porterfield
Nov 16 '05 #2
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. :)

Nov 16 '05 #3
Outstanding. Thank you Tom & Bruce for both posts. Both were well
detailed and well thought out.
Eric

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by John | last post: by
2 posts views Thread by tolisss | last post: by
8 posts views Thread by Robert W. | last post: by
11 posts views Thread by Simon Verona | last post: by
8 posts views Thread by Rlrcstr | last post: by
15 posts views Thread by satankidneypie | last post: by
2 posts views Thread by Luis Arvayo | last post: by
4 posts views Thread by =?Utf-8?B?Y2xhcmE=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.