Reflection on partially overriden property?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi,

Here is a short code snippet:

//*************************

public class Base
{
public virtual int X
{
get { return 0; }
set { }
}
}

public class Derived : Base
{
public override int X
{
set { } // only setter overriden here!
}
}

public class Test
{
static void Main()
{
Derived d = new Derived();
d.X = 0; // set OK
int x = d.X; // get OK

PropertyInfo pi = typeof(Derived).GetProperty("X");
pi.SetValue(d, 0, null); // set OK
x = (int)pi.GetValue(d, null); // get NOT OK (exception)
}
}

//*************************

I can access the getter for the X property in "normal" way, however I can't
do it using reflection (I get an exception "Property Get method was not
found"). It seems to be caused by "asymmetric" override (only setter modified
in the Derived class). The PropertyInfo object I get only refers to the
"setting part" of the property (for example, CanRead is false). It can be
used to invoke the setter but not the getter.

How to get value of such property through reflection? Or maybe I have missed
something?

Thx,
Jakub

.



Relevant Pages

  • Own implementation of GetHashCode()
    ... public override int GetHashCode() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how to wrap your brain around delegates?
    ... public class Derived: Base ... public void OnClick() ... EventArgs args = EventArgs.Empty; ...
    (borland.public.delphi.non-technical)
  • Calling parent class?
    ... I have a question on calling parent class... ... public class Derived: Base ... public override void SomeMethod() { ...
    (microsoft.public.dotnet.languages.csharp)
  • Calling parent class?
    ... I have a question on calling parent class... ... public class Derived: Base ... public override void SomeMethod() { ...
    (microsoft.public.dotnet.framework.aspnet)
  • What will be the constructor flow of control
    ... hard time understanding how C# can allow initialization of member variables ... public class D1: Base { ...
    (microsoft.public.dotnet.languages.csharp)