C# 可以将对象从字符串值强制转换为类类型
我做了一个这样的代码C# 可以将对象从字符串值强制转换为类类型,c#,casting,typeof,C#,Casting,Typeof,我做了一个这样的代码 private void MakeRowUsingMethodName(string pMethodName,params object[] var) { DataTable dt = dataSet1.Tables[0]; DataRow row = dt.NewRow(); row = dt.NewRow(); System.Reflection.MethodInfo pMethod = typ
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”;或者其他事情
类型(“ShellSection”).GetMethod(pMethodName);或
typeof(className).GetMethod(pMethodName)
我正在询问上述操作的可能性和方法。您可以尝试使用
Type.GetType
方法,它支持您按类字符串名称获取Type
Type.GetType(ShellSection).GetMethod(pMethodName);
而不是
typeof(ShellSection).GetMethod(pMethodName);
也许是这样的:我先读了,但我不懂英语,也不懂问题。我简单地告诉您如何使用它,并立即应用了它。谢谢你的评论。