[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

How to determine if a property is inherited or not using reflection?

Mark_google

6/19/2008 10:02:00 PM

I've tried using TypeDescriptor.GetProperties to get
PropertyDescriptors and I've tried <t>.GetProperties to get
PropertyInfo but I can't figure out how to determine for a given
property whether or not the property in question is inherited from an
ancestor or not.

For that matter, I can't determine the inheritance heirarchy either
(because that would be a workaround to see if my parent has the
property defined).

TIA,
4 Answers

Jeroen Mostert

6/19/2008 11:54:00 PM

0

Mark_google@nadigs.net wrote:
> I've tried using TypeDescriptor.GetProperties to get
> PropertyDescriptors and I've tried <t>.GetProperties to get
> PropertyInfo but I can't figure out how to determine for a given
> property whether or not the property in question is inherited from an
> ancestor or not.
>
Checking if .DeclaringType is the same as .ReflectedType should do the trick.

> For that matter, I can't determine the inheritance heirarchy either
> (because that would be a workaround to see if my parent has the
> property defined).
>
The inheritance hierarchy is actually an inheritance chain, since C# doesn't
have multiple inheritance. (Interfaces are "implemented" rather than
"inherited".) Simply following Type.BaseType will do.

Involving interfaces as well makes things trickier because .DeclaringType
will always show that a class declared the property, not an interface.
You'll have to enumerate interfaces and mappings with .GetInterfaces() and
..GetInterfaceMap() for a full picture.

--
J.
http://symbolsprose.bl...

Mark_google

6/20/2008 7:49:00 PM

0

Thanks for the reply,

> Checking if .DeclaringType is the same as .ReflectedType should do the trick.

I tried that and it seemed to work in most cases - except for one case
where I had a class ButtonControl that inherited an Infragistics
button control and the declaringtype and relfectedtype were my
buttoncontrol. I assumed it had to do w/ being in different
assemblies?

> The inheritance hierarchy is actually an inheritance chain, since C# doesn't
> have multiple inheritance. (Interfaces are "implemented" rather than
> "inherited".) Simply following Type.BaseType will do.

Excellent. Thank you.

> You'll have to enumerate interfaces and mappings with .GetInterfaces() and
> .GetInterfaceMap() for a full picture.

Thankfully, I can skip that part :)

Mark_google

6/20/2008 7:57:00 PM

0

> I tried that and it seemed to work in most cases - except for one case
> where I had a class ButtonControl that inherited an Infragistics
> button control and the declaringtype and relfectedtype were my
> buttoncontrol. I assumed it had to do w/ being in different
> assemblies?

Ah - this was because I wasn't actually inheriting the property, but
overriding it... duh..

Jeroen Mostert

6/20/2008 7:58:00 PM

0

digger wrote:
>> Checking if .DeclaringType is the same as .ReflectedType should do the trick.
>
> I tried that and it seemed to work in most cases - except for one case
> where I had a class ButtonControl that inherited an Infragistics
> button control and the declaringtype and relfectedtype were my
> buttoncontrol. I assumed it had to do w/ being in different
> assemblies?
>
That should not make any difference, actually (but I can't say it definitely
shouldn't, since I've never needed to do this). Could you give a minimal
example to reproduce this? Are you certain you actually implemented/overrode
the property and not accidentally declared a new one?

--
J.
http://symbolsprose.bl...