I was just on IM helping Kris Syverstad and Bob Brumfield with a little reflection problem. Along the way I asked him why he hadn't prototyped his problem in NUnit... I guess they didn't think of it.
Anyway, here is an NUnit test that demonstrates a couple of points that they were fighting:
- When referencing a nested type, you have to use the plus sign between the parent class and the nested type
- The Invoke code that Bob posted wasn't related to the problem.
- The Enum.Parse code that Bob posted wasn't related to the problem
Here's the code:
using System;
using System.Reflection;
using NUnit.Framework;
namespace NestedTypeReflectionTest
{
public class Foo
{
public enum Bar
{
Zero,
One,
Two,
Three
}
public Bar TheBar
{
get { return _theBar; }
set { _theBar = value; }
}
private Bar _theBar = Bar.Zero;
}
[TestFixture]
public class TheTest
{
private Assembly _assembly;
[SetUp]
public void Setup()
{
// For this example we will use GetExecutingAssembly() but you
// could also use Assembly.LoadFrom().
_assembly = Assembly.GetExecutingAssembly();
}
[Test(Description="This test shows that a period doesn't work " +
"for nested types.")]
[ExpectedException( typeof(TypeLoadException) )]
public void PeriodThrows()
{
System.Type t = _assembly.GetType( "NestedTypeReflectionTest.Foo.Bar",
true, false );
}
[Test(Description="This test shows that a plus-sign does work for " +
"nested types.")]
public void PlusDoesnt()
{
System.Type t = _assembly.GetType( "NestedTypeReflectionTest.Foo+Bar",
true, false );
Assert.IsTrue( t.IsEnum );
}
[Test(Description="This test shows using the type with Invoke.")]
public void Invoke()
{
System.Type t = _assembly.GetType( "NestedTypeReflectionTest.Foo+Bar",
true, false );
object aValue = Enum.Parse( t, "One" );
Foo foo = new Foo();
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance |
BindingFlags.SetProperty;
foo.GetType().InvokeMember( "TheBar", flags,
null, foo, new object[] { aValue });
Assert.AreEqual( Foo.Bar.One, foo.TheBar );
}
}
}