public class User { public string Name; public string Id; private string GetID() { return Id; } } [TestClass()] public class UserTests { [TestMethod()] public void ToStringTest() { User user= new User(); user.Id = "10"; PrivateObject pObj = new PrivateObject(user); var ret = (string)pObj.Invoke("GetID", null); Assert.AreEqual("10", ret); } }
出处:https://www.cnblogs.com/xixiuling/p/13957259.html