using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;namespace 使用typeof关键字获取类的内部结构
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            Type type = typeof(System.Int32);
            foreach (MethodInfo method in type.GetMethod())
            {
                richTextBox1.AppendText("方法名称:" + method.Name + Environment.NewLine);
                foreach (ParameterInfo parameter in method.GetParameters())
                {
                    richTextBox1.AppendText("参数:" + parameter.Name + Environment.NewLine);
                }
            }
        }
    }
}
错误提示是:“GetMethod”方法没有采用“0”个参数的重载
这个该怎么解决。。望哪位高手给出详细解答

解决方案 »

  1.   

    GetMethod方法必须带有参数,而你没给它参数。你是想得到一个类的所有方法集合?这个好像没有的。要查类的所有方法,直接看MSDN就行了。
      

  2.   

    你写错了  是type.GetMethods()  得到一个集合
    不是 type.GetMethod() 只是一个
      

  3.   

    错误很明显啦。type.GetMethod()),这个方法里面没有传递参数,提示很明显了。因为你这个方法又不是无参构造函数。所以。就报错啦。
      

  4.   

    应该是GetMethods哦。
    GetMethod需要参数。比如方法名字符串。
      

  5.   

    完整的代码如下:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Reflection;namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Type type = typeof(System.Int32);
                foreach (MethodInfo method in type.GetMethods()) 
                {
                    richTextBox1.AppendText("方法名称:" + method.Name + Environment.NewLine);
                    foreach (ParameterInfo parameter in method.GetParameters())
                    {
                        richTextBox1.AppendText("参数:" + parameter.Name + Environment.NewLine);
                    }
                }
            }
        }
    }
      

  6.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Reflection;namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Type type = typeof(System.Int32);
                foreach (MethodInfo method in type.GetMethods()) 
                {
                    richTextBox1.AppendText("方法名称:" + method.Name + Environment.NewLine);
                    foreach (ParameterInfo parameter in method.GetParameters())
                    {
                        richTextBox1.AppendText("参数:" + parameter.Name + Environment.NewLine);
                    }
                }
            }
        }
    }