473,804 Members | 3,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetPropert ies();
foreach (PropertyInfo pi in piArray)
{
ListViewItem item = new ListViewItem(pi .Name);
item.Tag = pi;
item.SubItems.A dd(pi.PropertyT ype.Name);
item.SubItems.A dd(pi.Module.Na me );
mLvProperties.I tems.Add(item);
}

Mar 16 '07 #1
3 4386
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.com wrote:
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.GetPropert ies();
foreach (PropertyInfo pi in piArray)
{
if (pi.DeclaringTy pe.Name == type.Name)
{
ListViewItem item = new ListViewItem(pi .Name);
item.Tag = pi;
item.SubItems.A dd(pi.PropertyT ype.Name);
item.SubItems.A dd(pi.Module.Na me);
item.SubItems.A dd(pi.Declaring Type.Name);
mLvProperties.I tems.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.co m>
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
2661
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 the property in the derived class, and I can use the derived class as expected through standard instantiation. The unexpected behavior occurs when I try to set gather the PropertyInfo for the derived class property via Reflection. I get an...
0
2201
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 Reflection.Emit which inherits from the object needed Person p = (Person)ProxyBuilder.Construct(typeof(Person));
10
7375
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 Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
3
2224
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 running through the properties, the PropertyInfo.Name does cycle through all the properties, however, if you check the PropertyInfo.GetType().ToString() it is always "System.Reflection.PropertyInfo". Which technically makes sense. What doesnt...
2
2877
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 'System.Collection.Generic.IList'. When performing something like: 'if (typeof(IList<>).IsAssignableFrom(propertyInfo.Type))' where 'propertyInfo' obviously refers to the 'InvoiceLines', the result is always 'false' because indeed 'IList<object>' and...
11
12801
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
6
1206
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 new routine that shows a messagebox with both objects' property values. below is my code. this is the actual comparison code: Dim Properties() As PropertyInfo = pType.GetProperties(BindingFlags.Public Or BindingFlags.Instance) Dim PropertyItem...
1
2255
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
2207
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 the following with my context object which represents the class containing the 200 properties: Type t = context.GetType(); foreach (PropertyInfo prop in t.GetProperties()
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.