By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,241 Members | 2,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,241 IT Pros & Developers. It's quick & easy.

reflection PropertyInfo. How to select properties of the derived class?

P: n/a
when enumerating thru the properties of a type, how do I select only
the properties of the derived class? In the example below, the
AddrBookPrompt class is derived from the Form class. When I enumerate
the properties I get all the properties of the base class + the
properties of my derived class.

thanks,
AddrBookPrompt abPmt = new AddrBookPrompt();
abPmt.FirstName = "Steve";
abPmt.LastName = "Richter";

System.Type type = abPmt.GetType();
PropertyInfo[] piArray = type.GetProperties();
foreach (PropertyInfo pi in piArray)
{
ListViewItem item = new ListViewItem(pi.Name);
item.Tag = pi;
item.SubItems.Add(pi.PropertyType.Name);
item.SubItems.Add(pi.Module.Name );
mLvProperties.Items.Add(item);
}

Mar 16 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Mar 16, 4:14 pm, "Steve Richter" <StephenRich...@gmail.comwrote:
when enumerating thru the properties of a type, how do I select only
the properties of the derived class? In the example below, the
AddrBookPrompt class is derived from the Form class. When I enumerate
the properties I get all the properties of the base class + the
properties of my derived class.
Use the overload which takes a BindingFlags, and make sure that you
include DeclaredOnly (as well as whatever other flags you want (eg
Instance, Public).

Jon
Mar 16 '07 #2

P: n/a
On Mar 16, 12:46 pm, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Mar 16, 4:14 pm, "Steve Richter" <StephenRich...@gmail.comwrote:
when enumerating thru the properties of a type, how do I select only
the properties of the derived class? In the example below, the
AddrBookPrompt class is derived from the Form class. When I enumerate
the properties I get all the properties of the base class + the
properties of my derived class.

Use the overload which takes a BindingFlags, and make sure that you
include DeclaredOnly (as well as whatever other flags you want (eg
Instance, Public).
where do I specify DeclaredOnly and BindingFlags?

I think I have the answer. I compare the "DeclaringType.Name" of the
property with the Type.Name of the type I am listing the properties
of:

AddrBookPrompt abPmt = new AddrBookPrompt();

System.Type type = abPmt.GetType();
PropertyInfo[] piArray = type.GetProperties();
foreach (PropertyInfo pi in piArray)
{
if (pi.DeclaringType.Name == type.Name)
{
ListViewItem item = new ListViewItem(pi.Name);
item.Tag = pi;
item.SubItems.Add(pi.PropertyType.Name);
item.SubItems.Add(pi.Module.Name);
item.SubItems.Add(pi.DeclaringType.Name);
mLvProperties.Items.Add(item);
}
}

thanks,

-Steve
Mar 16 '07 #3

P: n/a
Steve Richter <St************@gmail.comwrote:
Use the overload which takes a BindingFlags, and make sure that you
include DeclaredOnly (as well as whatever other flags you want (eg
Instance, Public).

where do I specify DeclaredOnly and BindingFlags?
DeclaredOnly is a member of the BindingFlags enum, which you can pass
to GetProperties.
I think I have the answer. I compare the "DeclaringType.Name" of the
property with the Type.Name of the type I am listing the properties
of:
You could do that, but it would be better to get GetProperties to
filter for you.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.