namespace Cao
{
public class Zheng
{
public int Bi(string mao, ref double shui)
{
MessageBox.Show(mao);
shui = 123.0;
return 0;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Cao.Zheng z = new Cao.Zheng();
string fnName = "Bi";
MethodInfo mi = z.GetType().GetMethod(fnName, BindingFlags.Instance | BindingFlags.Public);
if (mi != null)
{
string mao = "a";
double shui = 0.0;
object[] objArray = new object[] { mao, shui };
object obj = mi.Invoke(z, objArray);
shui = (double)objArray[1];
}
}
========================================================================================
function2
还没有解决吗?
如果名称空间及类的名称,如下可以执行指定类的方法:
string classFullName = "Cao.Zheng";//namespace+className
string methodName = "Bi";//methodName
Type type = Type.GetType(classFullName);//获取到类型
if (type != null)
{
object objInst = Activator.CreateInstance(type);//建立类型实例
if (objInst != null)
{
MethodInfo mi = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);//获取方法信息
if (mi != null)
{
string mao = "a";
double shui = 0.0;
object[] objArray = new object[] { mao, shui };
object obj = mi.Invoke(objInst, objArray);//调用方法
shui = (double)objArray[1];//获取值
}
}
}