This page last changed on Mar 22, 2007 by grahamhay.

If you have a SampleClass with a private field, property and method:

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
public class SampleClass
{
    private string _privateStringField = "Test";

    private string PrivateStringProperty
    {
        get { return _privateStringField; }
    }

    private int PrivateMethod(int x, int y)
    {
        return x + y;
    }
}

then here's how you would test it.

note

You will need to add a reference to MbUnit.Framework.2.0.dll in order to use the Reflector class.

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
using MbUnit.Framework;
using MbUnit.Framework.Reflection;

namespace MbUnitPrivate
{
    [TestFixture]
    public class SampleTests
    {
        Reflector _reflector;

        [SetUp]
        public void Init()
        {
            SampleClass sc = new SampleClass();
            _reflector = new Reflector(sc);
        }

        [Test]
        public void NonPublicField_Test()
        {
            object result = _reflector.GetNonPublicField("_privateStringField");
            Assert.AreEqual("Test", result);
        }

        [Test]
        public void NonPublicProperty_Test()
        {
            object result = _reflector.GetNonPublicProperty("PrivateStringProperty");
            Assert.AreEqual("Test", result);
        }

        [Test]
        public void PrivateMethod_Test()
        {
            object result = _reflector.RunPrivateMethod("PrivateMethod", 5, 7);
            Assert.AreEqual(12, result);
        }
    }
}

Thanks to Vadim Kreynin for the example.

Document generated by Confluence on Jun 11, 2007 11:56