我目前正在编写一个向导,希望在进入下一页之前验证每个页面。
我想通过在页面上的每个子控件上调用 Validate() 方法来阻止用户继续前进,并在其中任何一个失败时停止导航。
问题是每个子控件上的 Validate() 方法都是私有(private)方法,因此我无法直接访问它。任何人都可以给我一些关于如何使用反射从 TextBox 上的 Validate() 方法(例如)获取结果的建议吗?
非常感谢!
编辑:抱歉 - 应该指定,这是 Windows 窗体,.Net 2.0
最佳答案
如果页面恰好是 ContainerControl 实例,则只需调用 ValidateChildren 即可。如果没有,这似乎适用于单独的控件:
private void ValidateControl(Control control)
{
Type type = control.GetType();
type.InvokeMember("PerformControlValidation", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, control, new object[] { true });
}
关于c# - 使用反射调用控件的 Validate() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/268483/