I'm trying to use the InternalsVisibleTo to allow me to test a utility / helper method from a separate Test assembly. When I try and call an internal method with a dynamic parameter I get the error "RuntimeBinderException was unhandled ... is inaccessible due to its protection level."
I believe I am using the InternalsVisibleTo attribute correctly as I am able to test other internal methods that do not use dynamic parameters. The following code illustrates the scenario where only the TestInternalMethodWithDynamic test fails as shown below. I have repeated the tests using instance methods instead of static and that made no difference.
The .NET technology is Silverlight 5 and I am using the Silverlight Unit Test Framework to execute the tests. I need to use dynamic parameters due to the Excel automation requirements of the project.
Edit: I have tested the same call using .NET 4 class library assemblies and it is successful so the problem seems to be specific to Silverlight.
Example utility class...
public class Utility
{
internal static int InternalMethodWithDynamic(dynamic parameter) {
return (int)parameter;
}
internal static int InternalMethodWithInteger(int parameter) {
return parameter;
}
public static int PublicMethodWithDynamic(dynamic parmater) {
return (int)parmater;
}
public static int PublicMethodWithInteger(int parmater) {
return parmater;
}
}
And the test class...
[TestClass]
public class UtilityTest
{
[TestMethod]
public void TestInternalMethodWithDynamic() {
dynamic parameter = 10;
Assert.AreEqual(10, Utility.InternalMethodWithDynamic(parameter));
}
[TestMethod]
public void TestPublicMethodWithInteger() {
int parameter = 10;
Assert.AreEqual(10, Utility.PublicMethodWithInteger(parameter));
}
[TestMethod]
public void TestPublicMethodWithDynamic() {
dynamic parameter = 10;
Assert.AreEqual(10, Utility.PublicMethodWithDynamic(parameter));
}
[TestMethod]
public void TestInternalMethodWithInteger() {
int parameter = 10;
Assert.AreEqual(10, Utility.InternalMethodWithInteger(parameter));
}
}