1

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.

Silverlight Unit Test results

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));
    }
}
Martin Hollingsworth
  • 7,249
  • 7
  • 49
  • 51
  • 1
    Just FYI, I couldn't reproduce this using a simple Console App + Class Library. I was using .NET 4.5, not sure if that makes a difference. – Matt Smith Apr 19 '13 at 05:09
  • You must have missed my edit where I added that I tried it using .NET 4 class libraries and it passed OK. As I said in the edit I am pretty sure it is a Silverlight issue. Thanks for confirming it on .NET 4.5 though. @MattSmith – Martin Hollingsworth Apr 19 '13 at 05:41
  • Ah, yes, missed that. Interesting problem +1. – Matt Smith Apr 19 '13 at 05:43

1 Answers1

0

Workaround:

This isn't ideal but you can cheat a bit and still test the underlying logic of the method by passing the parameter as something other than dynamic (eg. one of the .NET Integral Types). I can't say I understand the dynamic type and implicit type conversion well enough to explain exactly what is happening but this seems to bypass the access modifier checks somehow. At least I don't need to make the method public. The modified test below now passes.

I think this is an acceptable workaround for my scenario but there may be some edge cases related to type conversion in other situations.

[TestClass]
public class UtilityTest
{
    [TestMethod]
    public void TestInternalMethodWithDynamic() {
        int parameter = 10;
        Assert.AreEqual(10, Utility.InternalMethodWithDynamic(parameter));
    }
}
Martin Hollingsworth
  • 7,249
  • 7
  • 49
  • 51