Sunday, March 11, 2012

Example of reflection getting the value of a private field

namespace sss
{
    class Program
    {
        static void Main(string[] args)
        {
            testReflection t = new testReflection();
            Type typ = Assembly.GetExecutingAssembly().GetType("sss.testReflection");
             int value = (int)typ.InvokeMember("i",
     BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
     null, t, null);
        }
    }
    public class testReflection
    {
        private int i = 78;
    }

No comments:

Post a Comment