471,319 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Reflection GetProperty Problem

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>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
//throws an AmbiguousMatchException
System.Reflection.PropertyInfo oPi =
typeof(ChildClass).GetProperty("Target");
}
}

//Parent Class
class ParentClass
{
public virtual object Target
{
get{return new object();}
}
}

//Child class which inherits from Parent class and hides inherited member
Target with the new keyword, and also changes the return type
class ChildClass : ParentClass
{
public new string Target
{
get{return "";}
}
}
}

What I am trying to do is get the property Target from the ChildClass. When
I do the GetProperty I am returned two PropertyInfo objects matching Target -
one from ParentClass and one from ChildClass. This happens because I am
changing the return type of Target from object to string in the ChildClass.
If the return type stays the same then only one PropertyInfo is returned.

Unfortunately, it's not an option for me to make the return types the same.
It's code written by someone else in a fairly big system and would likely
cause all sorts of problems. Has anyone got any suggestions how I can just
get the Target PropertyInfo from the ChildClass?

It's not feasible to use the declaring type attribute of property info as
there are many other classes in my real world code between ChildClass and
ParentClass that I haven't added to the example above.

I've also taken a look at the BindingFlags attribute parameter, but there is
no suitable BindingFlag that will return me the very top level Target object
- the closest is BindingFlags.DeclaredOnly, but this won't return me the
Target if I then inherit from ChildClass but then don't override the Target
object.

I hope I have explained this well, it's kinda difficult to get your head
round.

Any help would be much appreciated.

Stu
Dec 1 '05 #1
15 10935
Try calling GetProperties instead of GetProperty. Then loop through the
returned array and find the property you want.

Thi

Dec 1 '05 #2
Thanks for the quick reply,

I thought about using that but there are several problems. Obviously they
both have the same name, so I have to look at other properties on the
PropertyType attribute to get the right one, however, the candidates for this
would be...

1) Property Type property - I don't know what the return type of the
property is going to be so can't check against that, there are loads of
classes that implement the 'Parent Class', all with different return types.

2) Declaring Type Property - This is the type that the property is declared
in, so if I inherit from ChildClass and don't redefine the Target property,
the declaring type will be ChildClass and not the Type of the object I am
reflecting.

What I need to do, is somehow loop around the class hierarchy and find the
class at the highest (or lowest depending on how you look at it) level that
declares the Target property, but I don't know how without it being messy...

"Truong Hong Thi" wrote:
Try calling GetProperties instead of GetProperty. Then loop through the
returned array and find the property you want.

Thi

Dec 1 '05 #3
I've ended up doing this... I think it works right, it just looks messy...

using System;
using System.Reflection;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
//throws an AmbiguousMatchException
Type oType = typeof(SubChildClass);
System.Reflection.PropertyInfo[] oPis = oType.GetProperties();

System.Reflection.PropertyInfo oRequiredPi = null;

int nHierarchyLevel = -1;

foreach(PropertyInfo oPi in oPis)
{
if(oPi.Name=="Target")
{
//examine the basetypes, counting how many times we go up the hierarchy
int nTempHierarchyLevel = GetHierarchyLevel(oPi.DeclaringType, oType, 0);
if((nTempHierarchyLevel<=nHierarchyLevel)||(nHiera rchyLevel==-1))
{
nHierarchyLevel = nTempHierarchyLevel;
oRequiredPi = oPi;
}
}
}
}

private static int GetHierarchyLevel(Type tDeclaringType, Type
tTypeToMatch, int nHierarchyCount)
{
if(tDeclaringType==tTypeToMatch)
{
return nHierarchyCount;
}
else
{
nHierarchyCount++;
return GetHierarchyLevel(tDeclaringType, tTypeToMatch.BaseType,
nHierarchyCount);
}
}
}

//Parent Class
class ParentClass
{
public virtual object Target
{
get{return new object();}
}
}

//Child class which inherits from Parent class and hides inherited member
Target, and also changes the return type
class ChildClass : ParentClass
{
public new string Target
{
get{return "";}
}
}

//Child class which inherits from Parent class and hides inherited member
Target, and also changes the return type
class SubChildClass : ChildClass
{

}
}
"satankidneypie" wrote:
Thanks for the quick reply,

I thought about using that but there are several problems. Obviously they
both have the same name, so I have to look at other properties on the
PropertyType attribute to get the right one, however, the candidates for this
would be...

1) Property Type property - I don't know what the return type of the
property is going to be so can't check against that, there are loads of
classes that implement the 'Parent Class', all with different return types.

2) Declaring Type Property - This is the type that the property is declared
in, so if I inherit from ChildClass and don't redefine the Target property,
the declaring type will be ChildClass and not the Type of the object I am
reflecting.

What I need to do, is somehow loop around the class hierarchy and find the
class at the highest (or lowest depending on how you look at it) level that
declares the Target property, but I don't know how without it being messy...

"Truong Hong Thi" wrote:
Try calling GetProperties instead of GetProperty. Then loop through the
returned array and find the property you want.

Thi

Dec 1 '05 #4
>2) Declaring Type Property - This is the type that the property is declared
in, so if I inherit from ChildClass and don't redefine the Target property,
the declaring type will be ChildClass and not the Type of the object I am
reflecting.

Does DeclaringType.IsAssignableFrom(typeof(ChildClass)) help?

Dec 1 '05 #5
Use the following to only look at the property at the current class scope
(i.e. ChildClass):

System.Reflection.PropertyInfo oPi =
typeof(ChildClass).GetProperty("Target",
System.Reflection.BindingFlags.DeclaredOnly);

Marc

"satankidneypie" <sa************@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...
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>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
//throws an AmbiguousMatchException
System.Reflection.PropertyInfo oPi =
typeof(ChildClass).GetProperty("Target");
}
}

//Parent Class
class ParentClass
{
public virtual object Target
{
get{return new object();}
}
}

//Child class which inherits from Parent class and hides inherited member
Target with the new keyword, and also changes the return type
class ChildClass : ParentClass
{
public new string Target
{
get{return "";}
}
}
}

What I am trying to do is get the property Target from the ChildClass.
When
I do the GetProperty I am returned two PropertyInfo objects matching
Target -
one from ParentClass and one from ChildClass. This happens because I am
changing the return type of Target from object to string in the
ChildClass.
If the return type stays the same then only one PropertyInfo is returned.

Unfortunately, it's not an option for me to make the return types the
same.
It's code written by someone else in a fairly big system and would likely
cause all sorts of problems. Has anyone got any suggestions how I can just
get the Target PropertyInfo from the ChildClass?

It's not feasible to use the declaring type attribute of property info as
there are many other classes in my real world code between ChildClass and
ParentClass that I haven't added to the example above.

I've also taken a look at the BindingFlags attribute parameter, but there
is
no suitable BindingFlag that will return me the very top level Target
object
- the closest is BindingFlags.DeclaredOnly, but this won't return me the
Target if I then inherit from ChildClass but then don't override the
Target
object.

I hope I have explained this well, it's kinda difficult to get your head
round.

Any help would be much appreciated.

Stu

Dec 1 '05 #6
Seems I put things in wrong order, should be:
typeof(ChildClass).IsAssignableFrom(property.Decla ringType)

Dec 1 '05 #7
Whoops!
I meant:

System.Reflection.BindingFlags.DeclaredOnly |
System.Reflection.BindingFlags.GetProperty |
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance

as the binding flags... I didn't realise at the time because the exception
went away and the code ran to completion... I didn't notice that oPi was
null... it works now, however...

Marc

"Marc Gravell" <mg******@rm.com> wrote in message
news:Od**************@TK2MSFTNGP11.phx.gbl...
Use the following to only look at the property at the current class scope
(i.e. ChildClass):

System.Reflection.PropertyInfo oPi =
typeof(ChildClass).GetProperty("Target",
System.Reflection.BindingFlags.DeclaredOnly);

Marc

"satankidneypie" <sa************@discussions.microsoft.com> wrote in
message news:01**********************************@microsof t.com...
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>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
//throws an AmbiguousMatchException
System.Reflection.PropertyInfo oPi =
typeof(ChildClass).GetProperty("Target");
}
}

//Parent Class
class ParentClass
{
public virtual object Target
{
get{return new object();}
}
}

//Child class which inherits from Parent class and hides inherited member
Target with the new keyword, and also changes the return type
class ChildClass : ParentClass
{
public new string Target
{
get{return "";}
}
}
}

What I am trying to do is get the property Target from the ChildClass.
When
I do the GetProperty I am returned two PropertyInfo objects matching
Target -
one from ParentClass and one from ChildClass. This happens because I am
changing the return type of Target from object to string in the
ChildClass.
If the return type stays the same then only one PropertyInfo is returned.

Unfortunately, it's not an option for me to make the return types the
same.
It's code written by someone else in a fairly big system and would likely
cause all sorts of problems. Has anyone got any suggestions how I can
just
get the Target PropertyInfo from the ChildClass?

It's not feasible to use the declaring type attribute of property info as
there are many other classes in my real world code between ChildClass and
ParentClass that I haven't added to the example above.

I've also taken a look at the BindingFlags attribute parameter, but there
is
no suitable BindingFlag that will return me the very top level Target
object
- the closest is BindingFlags.DeclaredOnly, but this won't return me the
Target if I then inherit from ChildClass but then don't override the
Target
object.

I hope I have explained this well, it's kinda difficult to get your head
round.

Any help would be much appreciated.

Stu


Dec 1 '05 #8
Hi Marc,

If you then inherit from ChildClass with SubChildClass, but don't override
'Target', then Poperty info will be null (Example below)... I have classes
that do this too to contend with... (I am still doing it with the way I said
in one of my postings above - my third posting down I think)

This code returns null for the property info :

using System;
using System.Reflection;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
//throws an AmbiguousMatchException
Type oType = typeof(SubChildClass);
PropertyInfo oPi = oType.GetProperty("Target", BindingFlags.DeclaredOnly
| BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
}
}

//Parent Class
class ParentClass
{
public virtual object Target
{
get{return new object();}
}
}

//Child class which inherits from Parent class and hides inherited member
Target, and also changes the return type
class ChildClass : ParentClass
{
public new string Target
{
get{return "";}
}
}

//Child class which inherits from Parent class and hides inherited member
Target, and also changes the return type
class SubChildClass : ChildClass
{

}
}

Dec 1 '05 #9
You are right - sorry, I looked more at the code you provided than the
problem statement (my bad).

How about something like the following (written outside of the IDE, so no
guarantee it will compile, but you get the idea)
Type type = typeof(Whatever);
PropertyInfo pi = null;
while(type!=null && pi!=null) {
pi = type.GetProperty(); // params omitted for brevity, using DeclaredOnly
type = type.BaseType;
}
// pi is now either null or the "highest" version of the property

No idea if it would be quicker or not, but at least it is fairly obvious
what it is doing...

Marc

"satankidneypie" <sa************@discussions.microsoft.com> wrote in message
news:1D**********************************@microsof t.com...
Hi Marc,

If you then inherit from ChildClass with SubChildClass, but don't override
'Target', then Poperty info will be null (Example below)... I have classes
that do this too to contend with... (I am still doing it with the way I
said
in one of my postings above - my third posting down I think)

This code returns null for the property info :

using System;
using System.Reflection;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
//throws an AmbiguousMatchException
Type oType = typeof(SubChildClass);
PropertyInfo oPi = oType.GetProperty("Target", BindingFlags.DeclaredOnly
| BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
}
}

//Parent Class
class ParentClass
{
public virtual object Target
{
get{return new object();}
}
}

//Child class which inherits from Parent class and hides inherited member
Target, and also changes the return type
class ChildClass : ParentClass
{
public new string Target
{
get{return "";}
}
}

//Child class which inherits from Parent class and hides inherited member
Target, and also changes the return type
class SubChildClass : ChildClass
{

}
}

Dec 1 '05 #10
of course I mean while(type!=null && pi==null)

(time to get either some sleep or coffee... I guess it'll be the coffee...)

Marc

"Marc Gravell" <mg******@rm.com> wrote in message
news:Oy*************@TK2MSFTNGP15.phx.gbl...
You are right - sorry, I looked more at the code you provided than the
problem statement (my bad).

How about something like the following (written outside of the IDE, so no
guarantee it will compile, but you get the idea)
Type type = typeof(Whatever);
PropertyInfo pi = null;
while(type!=null && pi!=null) {
pi = type.GetProperty(); // params omitted for brevity, using
DeclaredOnly
type = type.BaseType;
}
// pi is now either null or the "highest" version of the property

No idea if it would be quicker or not, but at least it is fairly obvious
what it is doing...

Marc

"satankidneypie" <sa************@discussions.microsoft.com> wrote in
message news:1D**********************************@microsof t.com...
Hi Marc,

If you then inherit from ChildClass with SubChildClass, but don't
override
'Target', then Poperty info will be null (Example below)... I have
classes
that do this too to contend with... (I am still doing it with the way I
said
in one of my postings above - my third posting down I think)

This code returns null for the property info :

using System;
using System.Reflection;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
//throws an AmbiguousMatchException
Type oType = typeof(SubChildClass);
PropertyInfo oPi = oType.GetProperty("Target", BindingFlags.DeclaredOnly
| BindingFlags.GetProperty | BindingFlags.Public |
BindingFlags.Instance);
}
}

//Parent Class
class ParentClass
{
public virtual object Target
{
get{return new object();}
}
}

//Child class which inherits from Parent class and hides inherited member
Target, and also changes the return type
class ChildClass : ParentClass
{
public new string Target
{
get{return "";}
}
}

//Child class which inherits from Parent class and hides inherited member
Target, and also changes the return type
class SubChildClass : ChildClass
{

}
}


Dec 1 '05 #11
>I am still doing it with the way I said
in one of my postings above - my third posting down I think

Have you tried Type.IsAssignableFrom as I suggested? It is much simpler
than the way you do with GetHierarchyLevel.
// Determine if typeToCheck is type1 or a subclass of type1
private bool IsSelfOrChild(Type type1, Type typeToCheck)
{
return type1.IsAssignableFrom(typeToCheck) ;
}

Thi

Dec 2 '05 #12
If I understand the original question correctly (which, admittedly, I didn't
at first), this is more an issue of getting the highest-level version of a
property on aclass-hierarchy (where multiple of the same name may exist via
the new operator, and the top-most class may not define it itself at all).

Given this, I'm not sure that IsAssignableFrom helps much; if it was a case
of getting the correctly typed version, then this can be done using one of
the overloads of GetProperty (that accepts a Type to represent the declared
typ).

Marc

"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I am still doing it with the way I said
in one of my postings above - my third posting down I think

Have you tried Type.IsAssignableFrom as I suggested? It is much simpler
than the way you do with GetHierarchyLevel.
// Determine if typeToCheck is type1 or a subclass of type1
private bool IsSelfOrChild(Type type1, Type typeToCheck)
{
return type1.IsAssignableFrom(typeToCheck) ;
}

Thi

Dec 2 '05 #13
As I understand, what the OP wanted is that:
- if call with ChildClass, get the Target property as defined in
ChildClass
- if call with SubChildClass and SubChildClass does not override
Target, get the Target property as defined in ChildClass
- if call with SubChildClass and SubChildClass overrides Target, get
the Target property as defined in SubChildClass
- Never get the Target property defined in ParentClass (which
ChildClass extends).

Thus, he could write it as simple as this:
PropertyInfo GetTargetProperty(Type t)
{
if (typeof(ChildClass).IsAssignableFrom(t))
{
// not ChildClass or one of its children
return null;
}

return t.GetProperty("Target");
}

Because the original poster decide to get all properties, and walk the
hierachy to see if a class is a child of other class, he can also
obtain that by simply call IsAssgnableFrom.

Thi

Dec 2 '05 #14
Sorry, I forget to put "!" in the first if, and wrongly called
GetProperty instead of GetProperties as I suggested in my first reply.

Here is the updated version. I should work.
I tested in my machine.

PropertyInfo GetTargetProperty(Type t)
{
if (!typeof(Type1).IsAssignableFrom(t))
{
// not ChildClass or one of its children
return null;
}

PropertyInfo[] ps = t.GetProperties();
foreach (PropertyInfo p in ps)
{
if (p.Name == "Target" && p.PropertyType == typeof(string))
{
return p;
}
}

// not found, return nul
return null;
}

Thi

Dec 2 '05 #15
Hi Stu,

Check out this article on Reflection, it might help you answer your
question.

http://www.developersdex.com/gurus/articles/739.asp

Happy Coding,

Stefan
C# GURU
www.DotNETovation.com

*** Sent via Developersdex http://www.developersdex.com ***
Dec 3 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Laser Lu | last post: by
4 posts views Thread by Nicolas | last post: by
reply views Thread by John Wright | last post: by
reply views Thread by Marc Vangrieken | last post: by
7 posts views Thread by BK | last post: by
5 posts views Thread by Nightfall | last post: by
reply views Thread by rosydwin | last post: by

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.