Setting a field on an object by reflection
private static void SetFieldViaReflection(
object obj,
string fieldName,
object value)
{
Type InstanceType = Instance.GetType();
FieldInfo fi = InstanceType.GetField(fieldName,
BindingFlags.NonPublic | BindingFlags.Instance);
if (fi != null)
{
fi.SetValue(obj, value);
}
}
SetFieldByReflection(object, "m_field", somevalue as object);
No comments:
Post a Comment