Mega Code Archive
Set Non-Pubic Field
using System;
using System.Reflection;
namespace Homelidays.Web.SessionService.Tests
{
///
/// A helper class that eases reflection operations.
/// voici Les méthodes à implémenter au fur et à mesure des besoins:
/// - internal static object CallNonPublicStaticMethod(string className, string methodName)
/// - internal static object InstanciateNonPublicClass(string className)
///
internal static class ReflectionUtility
{
///
/// Set the value of a non public field.
///
/// Object whose fild should be set.
/// Name of the field to set.
/// Value to set the field with.
internal static void SetNonPublicField(object instance, string fieldName, object value)
{
Type type = instance.GetType();
var parameters = new object[1]; // alwasy 1 because a property setter could only have one parameter
parameters[0] = value;
type.InvokeMember(
fieldName,
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetField,
null,
instance,
parameters);
}
}
}