public static object SetInstanceFieldOrProperty(object target, string fieldName, object val)
{
Type t = target.GetType();
FieldInfo field = GetField(t, fieldName, false);
if (field != null)
{
field.SetValue(target, val);
return val;
}
PropertyInfo prop = GetProperty(t, fieldName, false);
if (prop != null)
{
prop.SetValue(target, val, new object[0]);
return val;
}
throw new ArgumentException(String.Format("No matching field/property found: {0} for {1}", fieldName, t));
}