可以将对象从字符串值转换为类类型

Can possible casting object from string-value to classType

我做了一个这样的代码。

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);
    }

现在工作正常,但我想更灵活地使用其他 class。

但这部分对我来说很难。

System.Reflection.MethodInfo pMethod = typeof(ShellSection).GetMethod(pMethodName);

"ShellSection" 是 ClassName,

所以我想知道是否可以将 ClassName 更改为字符串值。

而且我不知道这个方法可以转换成全局方法。

如果只修改这部分,我可以做出更灵活的代码。

PS.

我会解决我的问题。

字符串class名称="ShellSection";或其他事情

typeof("ShellSection").GetMethod(pMethodName);或

typeof(className).GetMethod(pMethodName);

我问的是上述的可能性和操作方法

您可以尝试使用 Type.GetType 方法,它支持您通过 class 字符串名称获得 Type

Type.GetType(ShellSection).GetMethod(pMethodName);

而不是

typeof(ShellSection).GetMethod(pMethodName);