As disgussed in .NET Reflection set private property one can set a property with a private setter. But when the property is defined in a base class, System.ArgumentException is thrown : "Property set method not found".
An example can be:
using System;
class Test
{
public DateTime ModifiedOn { get; private set;}
}
class Derived : Test
{
}
static class Program
{
static void Main()
{
Derived p = new Derived ();
typeof(Derived).GetProperty("ModifiedOn").SetValue(
p, DateTime.Today, null);
Console.WriteLine(p.ModifiedOn);
}
}
Does anyone know a way to tackle this situation?
Edit: The example given is a simple illustration of the problem. In the real world scenario, I do not know if the property is defined in a base class, or defined in the base of the base class.