我做了一个这样的代码。
private void MakeRowUsingMethodName(string pMethodName,params object[] var)
{
DataTable dt = dataSet1.Tables[0];
DataRow row = dt.NewRow();
row = dt.NewRow();
System.Reflection.MethodInfo pMethod = typeof(ShellSection).GetMethod(pMethodName);
int paramIndex = 0;
string paramType = string.Empty;
string paramName = string.Empty;
int paramCount = pMethod.GetParameters().Length;
string MethodName = pMethod.Name;
foreach (ParameterInfo pParameter in pMethod.GetParameters())
{
paramIndex = pParameter.Position;
paramName = pParameter.Name;
paramType = pParameter.ParameterType.Name;
row[paramIndex] = var[paramIndex];
}
dt.Rows.Add(row);
}
它现在工作正常,但我想要更多的灵活性使用其他类。
但这部分对我来说很难。
System.Reflection.MethodInfo pMethod = typeof(ShellSection).GetMethod(pMethodName);
“ShellSection”是类名,
所以我想知道它可以将 ClassName 更改为字符串值。
而且我不知道这种方法转换为全局方法。
如果只有这部分可以修改,我可以做出更灵活的代码。
附注。
我会解决我的问题。
string className = "ShellSection"; 或其他东西
typeof("ShellSection").GetMethod(pMethodName); 或者
typeof(className).GetMethod(pMethodName);
我在询问上述操作的可能性和方法。
您可以尝试使用Type.GetType
它支持的方法,Type
通过类字符串名称获取。
Type.GetType(ShellSection).GetMethod(pMethodName);
代替
typeof(ShellSection).GetMethod(pMethodName);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句