Private Accessors in Visual Studio 2012

岁酱吖の 提交于 2019-12-07 03:40:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!