Returns the value of the private member specified. - CSharp System.Reflection

CSharp examples for System.Reflection:Member

Description

Returns the value of the private member specified.

Demo Code


using System.Reflection;
using System;/*from   w  w  w  .  jav  a  2 s  . c  om*/

public class Main{
        /// <summary>
      /// Returns the value of the private member specified.
      /// </summary>
      /// <param name="fieldName">Name of the member.</param>
      /// <param name="source">The object that contains the member.</param>
      public static T GetPrivateInstanceFieldValue<T>(string fieldName, object source)
      {
         FieldInfo field = source.GetType().GetField(fieldName, BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);
         if(field != null)
         {
            return (T)field.GetValue(source);
         }
         return default(T);
      }
}

Related Tutorials