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