如何根据方法名字符串反射执行该方法 例如:public float Example(int a, string b, bool c){ .... return 0;}string methodName = "Example";下来如何根据methodName来执行该方法(已知参数类型和返回类型确定) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.chinaunix.net/u/1485/showart_321335.html type.InvokeMember("MethodName", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, new object[] {param}); http://blog.chinaunix.net/u/1485/showart_321335.html this.GetType().InvokeMember("HelloC", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, this, null); /// <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); 调用 楼主自己改下应该就可以用了 关于失败重试的问题 报错 '无法显示反汇编 表达式还没有翻译为本机代码' 请教:C#做SAP接口问题 C#中 OleDbDataAdapter.Update()出现标识符超出范围?? 一个关于访问性的问题~~~~ 关于 Windows Form 用户控件自定义属性的问题,求助,谢谢! 求助!!哪位高人会PHP,帮我把这段代码转成C#的啊?急求! 还是不明白C#的foreach怎用? 关于Winform中调用OCX控件中事件问题 【分享】如何不翻墙的情况下使用Google?【开源】 如何声明和获取不定长的byte数组? 求设计思路,显示问题
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null,
obj, new object[] {param});
/// 一般插件接口
/// </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); 调用 楼主自己改下应该就可以用了