473,394 Members | 1,701 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.

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 4401
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
2
by: tolisss | last post by:
Hi could someone explain to me why the followind code fails using System; using System.Collections; using System.Reflection; using MbUnit.Core.Framework; using MbUnit.Framework; namespace...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
11
by: Simon Verona | last post by:
How can I write a line of code that sets an object to be an object thats contained on the parent form. Basically, I have a usercontrol, where one of the properties that I have added that can be...
8
by: Rlrcstr | last post by:
If I have some code behind a form and I want to be able to pass an object to it... any of several user defined classes that I have... and then display the values in the member variables of the...
15
by: satankidneypie | last post by:
Hi, I'm going to start this off with some code as it'll make it easier to explain afterwards... using System; namespace ConsoleApplication1 { /// <summary>
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
4
by: =?Utf-8?B?Y2xhcmE=?= | last post by:
Hi all, what is the difference between the late binding and reflection? clara -- thank you so much for your help
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.