问题
as far as I know Visual Studio 2012 will not support Private Accessors. Can you give me some infos on possible alternatives? PrivateObject.Invoke() is not the best solution for me.
Thank you in advance!
回答1:
Mark your members as "internal" and use the InternalsVisibleTo attribute. Simple to use and no type safety issues.
回答2:
Maybe this posting Home-made Private Accessor for Visual Studio 2012+ will help you creating your own Private Accessor.
Regards,
Stefan
回答3:
I started using the Dynamic Private Accessor feature that is part of the nuget package Chaining Assertion for MSTest and I am rather happy with it. One looses strong typing but at least the syntax is still readable. This works with the dynamic feature. There are also packages for other testing frameworks. Testing code looks like this:
var target = CreateMyObjectUnderTest();
dynamic dynamicTarget = target.AsDynamic();
Assert.AreEqual("abc", dynamicTarget.MyPrivateProperty);
Assert.AreEqual("xyz", dynamicTarget.PrivateMethod(123));
来源:https://stackoverflow.com/questions/11361647/private-accessors-in-visual-studio-2012