例如:public float Example(int a, string b, bool c)
{
    ....
    return 0;
}string methodName = "Example";下来如何根据methodName来执行该方法(已知参数类型和返回类型确定)

解决方案 »

  1.   

    http://blog.chinaunix.net/u/1485/showart_321335.html
      

  2.   

    type.InvokeMember("MethodName",
                                              BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null,
                                              obj, new object[] {param});
      

  3.   

    http://blog.chinaunix.net/u/1485/showart_321335.html
      

  4.   

    this.GetType().InvokeMember("HelloC", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, this, null);
      

  5.   

      /// <summary>
        /// 一般插件接口
        /// </summary>
        public interface IPlugIn
        {
            string Name
            {
                get;
                set;
            }
            //form图标
            Icon FrmIcon
            {
                get;
                set;
            }
            //当前的主程序实例
            IPluginHost Host
            {
                get;
                set;
            }
            //显示窗体
            void Show();
        }    /// <summary>
        /// 主程序接口
        /// </summary>
        public interface IPluginHost
        {
            bool Register(IPlugIn iPlugin);
        } }  public abstract class PlugIn : IPlugIn
        {
            private IPluginHost host;
            private string stringName;
            private Icon frmIcon;     //form图标
            public PlugIn(Icon frmIcon)
            {
                this.frmIcon = frmIcon;
            }
            public string Name
            {
                get { return stringName; }
                set { stringName = value; }
            }        public Icon FrmIcon
            {
                get { return frmIcon; }
                set { frmIcon = value; }
            }        public IPluginHost Host
            {
                get
                {
                    return host;
                }
                set
                {
                    host = value;
                    host.Register(this);
                }
            }
            public abstract void Show();
        }
    //具体调用 如下 private void LoadAssembly(params object[] args)
            {
                string expandName = ".dll";
                string filePath = Application.StartupPath + "\\PlusIn";
                if (!Directory.Exists(filePath)) return;
                string[] dllFiles = Directory.GetFiles(filePath, "*" + expandName);
                if (dllFiles.Length == 0)
                {
                    this.rbti_plusin.Visible = false;
                    this.rbar_plusinItems.Visible = false;
                }
                iPlugIn = new IPlugIn[dllFiles.Length];
                for (int i = 0; i < dllFiles.Length; i++)
                {
                    int startIndex = dllFiles[i].LastIndexOf("\\") + 1;
                    int endIndex = dllFiles[i].IndexOf(expandName) - dllFiles[i].LastIndexOf("\\") - 1;                string dllFileName = dllFiles[i].Substring(startIndex, endIndex);
                    //加载dll文件
                    try
                    {
                        Assembly ass = Assembly.LoadFile(dllFiles[i]);
                        Type objType = null;
                        if (ass != null)
                        {
                            objType = ass.GetType(dllFileName + ".MyPlugIn");
                        }
                        // 产生一个实例
                        if (objType != null)
                        {
                            iPlugIn[i] = (IPlugIn)Activator.CreateInstance(objType, args);
                            iPlugIn[i].Name = dllFileName;
                            iPlugIn[i].Host = this;
                        }
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message + ex.Source + ex.StackTrace); }
                }
            }
    。    this.LoadAssembly(null); 调用    楼主自己改下应该就可以用了