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
Thi
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...
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
{
System.Reflection.PropertyInfo oPi =
typeof(ChildClass).GetProperty("Target",
System.Reflection.BindingFlags.DeclaredOnly);
Marc
"satankidneypie" <satanki...@discussions.microsoft.com> wrote in message
news:01F162E4-E2E5-4776...@microsoft.com...
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" <mgra...@rm.com> wrote in message
news:Od6RoDn9...@TK2MSFTNGP11.phx.gbl...
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
{
}
}
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" <satanki...@discussions.microsoft.com> wrote in message
news:1D7C9151-CC7C-4ABC...@microsoft.com...
(time to get either some sleep or coffee... I guess it'll be the coffee...)
Marc
"Marc Gravell" <mgra...@rm.com> wrote in message
news:OyXlsOq...@TK2MSFTNGP15.phx.gbl...
Thi
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" <thi...@gmail.com> wrote in message
news:1133507067.5...@z14g2000cwz.googlegroups.com...
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
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
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 ***