Mega Code Archive

 
Categories / C# / Reflection
 

Get Non-Pubic Property

using System; using System.Reflection; namespace Homelidays.Web.SessionService.Tests {     /// <summary>     /// 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)     /// </summary>     internal static class ReflectionUtility     {         /// <summary>         /// Call a non public property of an object.         /// </summary>         /// <param name="instance">Object whose method to call is private.</param>         /// <param name="propertyName">Name of the property to call.</param>         /// <returns>The object returned by the private method to call.</returns>         internal static object GetNonPubicProperty(object instance, string propertyName)         {             Type type = instance.GetType();             var return_object = type.InvokeMember(                 propertyName,                 BindingFlags.Instance | BindingFlags.GetProperty,                 null,                 instance,                 null);             return return_object;         }     } }