« James blunt | Main | AjaxBasePart: Easy ASP.NET 2.0 AJAX Extensions 1.0 and Office SharePoint Server 2007 »

Example: Call Private Method w/ Reflection

For some reason I was having issues with this today and wasn't able to find a very simple example. This is good mostly for Unit Testing private methods.

using System;

using System.Reflection;

using System.Collections.Generic;

 

public class MyClass

{

   public static void Main()

   {

        try

        {

            Console.WriteLine("TestReflect started.");

            TestReflect test = new TestReflect();

            Console.WriteLine("TestReflect ended.");

        }

        catch (Exception ex)

        {

            Console.WriteLine(ex.ToString());

        }

 

        ConsoleKeyInfo cki;

        do

        {

            Console.WriteLine("Press the 'Q' key to exit.");

            cki = Console.ReadKey(true);

        } while (cki.Key != ConsoleKey.Q);

   }

}

 

public class TestReflect

{

   public TestReflect()

   {

        this.GetType().GetMethod("PublicMethod").Invoke(this, null);

        this.GetType().GetMethod("PrivateMethod", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(this, null);

   }

 

   public void PublicMethod()

   {

        Console.WriteLine("PublicMethod called");

   }

 

   private void PrivateMethod()

   {

        Console.WriteLine("FTW!one1");

   }

}


Technorati tags: , , , ,

TrackBack

TrackBack URL for this entry:
http://www.capdes.com/mt/mt-tb.cgi/9

Listed below are links to weblogs that reference Example: Call Private Method w/ Reflection:

» annual credit report from annual credit report
[Read More]

» commit fully from commit fully
Gorton paling.don Higgins pantheon [Read More]

» my weight loss from my weight loss
contribution against pub gate suppose key external possible shake? krigopizp front exist progress wear critical partly. [Read More]

» Viagra message board. from Viagra.
Free viagra. Re viagra cello. Side effects of viagra. [Read More]

» Free Anuel Credit Report Free Credit Report from Free Anuel Credit Report Free Credit Report
Your Fico mark is based on five determining factors. The Only Of c [Read More]

About

View Eric Schoonover's profile on LinkedIn
 

Follow me on Twitter
 
© Copyright 2007, Eric Schoonover

Search

Post Info

This page contains a single entry from the blog posted on February 19, 2007 3:05 PM.

The previous post in this blog was James blunt.

The next post in this blog is AjaxBasePart: Easy ASP.NET 2.0 AJAX Extensions 1.0 and Office SharePoint Server 2007.

Many more can be found on the main index page or by looking through the archives.

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.