I have the following class in a Universal Windows exe project (actual functionality not included).
sealed class DataPage
{
public DataPage(dynamic page)
{
Data = page;
}
public dynamic Data { get; private set; }
}
This assembly includes the InternalsVisibleTo
attribute so I can unit test internal classes:
[assembly:InternalsVisibleTo("PictureFrame.UnitTests")]
The unit test project (Universal Windows Unit Test project) compiles just fine. The test below however will throw an exception when passing a dynamic
object to the DataPage
constructor. It does not except when the instance passed is of type object
.
[TestMethod]
public void ReproException()
{
var o = new object();
var page = new DataPage(o); // this works fine
var d = (dynamic)o;
var page1 = new DataPage(d); // this throws exception
}
The exception thrown is: 'PictureFrame.Model.DataPage.DataPage(object)' is inaccessible due to its protection level.
I can of course mark the internal classes public if they interact with dynamic objects but is this something that is expected to work? This is the first time I've used InternalsVisibleTo
and dynamic
and am a bit surprised by this behavior, but perhaps it is an edge case from the DLR perspective.
(In real life the dynamic object is an ExpandoObject
deserialized from json.)