For some testing purpose, I need to set value for readonly property. Here is the code...
public void SetPrivatePropertyValue<T>(object obj, string propName, T val)
{
Type t = obj.GetType();
var prop = t.GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (prop == null)
throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName));
t.InvokeMember(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, obj, new object[] { val });
}
but that giving me error like
Method '<methodName>' not found
I also tried with
prop.SetValue(obj, true, null);
but error is:
Property set method not found
Can anyone tell me what's wrong here.
Aucun commentaire:
Enregistrer un commentaire