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