Reflection GetProperty Problem

G

Guest

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
 
T

Truong Hong Thi

Try calling GetProperties instead of GetProperty. Then loop through the
returned array and find the property you want.

Thi
 
G

Guest

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...
 
G

Guest

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)||(nHierarchyLevel==-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
{

}
}
 
T

Truong Hong Thi

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?
 
M

Marc Gravell

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
 
T

Truong Hong Thi

Seems I put things in wrong order, should be:
typeof(ChildClass).IsAssignableFrom(property.DeclaringType)
 
M

Marc Gravell

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 said:
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 said:
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
 
G

Guest

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
{

}
}
 
M

Marc Gravell

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
 
M

Marc Gravell

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 said:
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 said:
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
{

}
}
 
T

Truong Hong Thi

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
 
M

Marc Gravell

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
 
T

Truong Hong Thi

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
 
T

Truong Hong Thi

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top