想动态调用本类的成员函数。之前是用反射实现的,但是代价有点高。现在想换成委托public partial class CLS1 : BasePage
{
public delegate bool DBOpdelegate(SqlCommand objCmd, string strHead); //declare
//........
private InvokeFunc(string strTbNm, SqlCommand objCmd)
{
DBOpdelegate DbOp = Delegate.CreateDelegate(typeof(DBOpdelegate),typeof(CLS1).GetMethod("DBOp" + strTbNm + "Update")) as DBOpdelegate;
if (!DbOp(objCmd, "BACKUP_"))
{
//,,,,
}
}
}
以上代码运行会报目标函数无法关联的错误,请问如何修改?
{
public delegate bool DBOpdelegate(SqlCommand objCmd, string strHead); //declare
//........
private InvokeFunc(string strTbNm, SqlCommand objCmd)
{
DBOpdelegate DbOp = Delegate.CreateDelegate(typeof(DBOpdelegate),typeof(CLS1).GetMethod("DBOp" + strTbNm + "Update")) as DBOpdelegate;
if (!DbOp(objCmd, "BACKUP_"))
{
//,,,,
}
}
}
以上代码运行会报目标函数无法关联的错误,请问如何修改?
typeof(DBOpdelegate),
this,
"DBOp" + strTbNm + "Update") as DBOpdelegate;
或者Delegate.CreateDelegate(Type, Object, MethodInfo)