473,385 Members | 1,734 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,385 software developers and data experts.

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

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

Similar topics

1
by: Steve | last post by:
Hello, I'm encountering an unexpected behavior when using the "new" modifier in a derived class to hide an inherited base class property. I use "new" intentionally so I can change the Type of...
0
by: Nigel Sampson | last post by:
ey all, I'm in the process of creating a method of tracking whenever an objects properties are changed. Since its not possible to alter existing types I decided to used a proxy generated using...
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...
3
by: Roger Webb | last post by:
Hey, I'm attempting to use reflection in order to get the types of properties in a class. However, I seem to be having trouble determining exactly which type I am getting. In the foreach of...
2
by: Marc | last post by:
Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine> InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by reflection that 'InvoiceLines' is a...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
6
by: ECathell | last post by:
I have a routine that compares 2 intances of the same object to see if there are any changes. The problem is that even though there are no changes, it tells me there are. I have even tried running a...
1
by: darin_dimitrov | last post by:
Is it possible to determine if a property is read-only, write-only or read-write with reflection? Does MemberInfo class contains such information? Thanks
2
by: Martin Eckart | last post by:
Hi guys, I have a class which contains ~ 200 properties. Out of those 200 properties I need to access 10 (which I know beforehand already) via reflection in another class. Currently I am doing...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.