http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtypeclassinvokemembertopic3.asp

解决方案 »

  1.   

    public abstract object InvokeMember(
       string name,
       BindingFlags invokeAttr,
       Binder binder,
       object target,
       object[] args,
       ParameterModifier[] modifiers,
       CultureInfo culture,
       string[] namedParameters
    );this function is normally used in InteropParameterModifier allows you to specify whether the corresponding parameter is [in], [out], [lcid], [retval], or [optional], for example, if you have a parameter is [in,out], you need to doParameterModifier mods = new ParameterModifier(2);
    mods[0] = true;
    mods[1] = true;then pass in as the 6th parameter:
     new ParameterModifier[]{mods}CultureInfo represents the globalization locale to usenamedParameters is used when you want to pass the parameters by names instead of by positions
      

  2.   

    给你一个我很早以前写的调用invokemember验证窗体上所有控件不能为空的例子,没有实际意义,仅供参考。
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Reflection;namespace WindowsApplication11
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TabControl tabControl1;
    private System.Windows.Forms.TabPage tabPage1;
    private System.Windows.Forms.TabPage tabPage2;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.ErrorProvider errorProvider1; /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.tabControl1 = new System.Windows.Forms.TabControl();
    this.tabPage1 = new System.Windows.Forms.TabPage();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.tabPage2 = new System.Windows.Forms.TabPage();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
    this.tabControl1.SuspendLayout();
    this.tabPage1.SuspendLayout();
    this.tabPage2.SuspendLayout();
    this.SuspendLayout();
    // 
    // tabControl1
    // 
    this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.tabPage1,
      this.tabPage2});
    this.tabControl1.Location = new System.Drawing.Point(80, 56);
    this.tabControl1.Name = "tabControl1";
    this.tabControl1.SelectedIndex = 0;
    this.tabControl1.Size = new System.Drawing.Size(376, 232);
    this.tabControl1.TabIndex = 0;
    // 
    // tabPage1
    // 
    this.tabPage1.Controls.AddRange(new System.Windows.Forms.Control[] {
       this.textBox3});
    this.tabPage1.Location = new System.Drawing.Point(4, 21);
    this.tabPage1.Name = "tabPage1";
    this.tabPage1.Size = new System.Drawing.Size(368, 207);
    this.tabPage1.TabIndex = 0;
    this.tabPage1.Text = "tabPage1";
    // 
    // textBox3
    // 
    this.textBox3.Location = new System.Drawing.Point(8, 48);
    this.textBox3.Name = "textBox3";
    this.textBox3.Size = new System.Drawing.Size(104, 21);
    this.textBox3.TabIndex = 0;
    this.textBox3.Text = "";
    this.textBox3.Validating += new System.ComponentModel.CancelEventHandler(this.textBox3_Validating);
    // 
    // tabPage2
    // 
    this.tabPage2.Controls.AddRange(new System.Windows.Forms.Control[] {
       this.textBox1});
    this.tabPage2.Location = new System.Drawing.Point(4, 21);
    this.tabPage2.Name = "tabPage2";
    this.tabPage2.Size = new System.Drawing.Size(368, 207);
    this.tabPage2.TabIndex = 1;
    this.tabPage2.Text = "tabPage2";
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(216, 56);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "";
    this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(192, 312);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(104, 21);
    this.textBox2.TabIndex = 1;
    this.textBox2.Text = "";
    this.textBox2.Validating += new System.ComponentModel.CancelEventHandler(this.textBox2_Validating);
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(376, 320);
    this.button1.Name = "button1";
    this.button1.TabIndex = 2;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(528, 397);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button1,
      this.textBox2,
      this.tabControl1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.tabControl1.ResumeLayout(false);
    this.tabPage1.ResumeLayout(false);
    this.tabPage2.ResumeLayout(false);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void ValidateAllControl(Control c)
    {
    foreach(Control ch in c.Controls)
    {
    typeof(Control).InvokeMember("OnValidating",BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.InvokeMethod,null,ch,new object[]{new CancelEventArgs()});
    ValidateAllControl(ch);
    }
    }
            
    private bool IsNull(Control control,string s)
    {
    if(s.Trim()=="")
    {
    this.errorProvider1.SetError(control,"不能为空");
    return false;
    }
    else
    {
    this.errorProvider1.SetError(control,"");
    return true;
    }
    }
    private void textBox3_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if(IsNull(this.textBox3,textBox3.Text))
    this.errorProvider1.SetError(textBox3,"");
    else
    return;
    } private void textBox2_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
    if(IsNull(this.textBox2,textBox2.Text))
    this.errorProvider1.SetError(textBox2,"");
    else
    return;
    } private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
    if(IsNull(this.textBox1,textBox1.Text))
    this.errorProvider1.SetError(textBox1,"");
    else
    return;
    } private void button1_Click(object sender, System.EventArgs e)
    {
    this.ValidateAllControl(this);
    }
    }
    }