【问题标题】:Can possible casting object from string-value to classType [duplicate]可以将对象从字符串值转换为类类型 [重复]
【发布时间】:2019-04-11 12:43:36
【问题描述】:

我做了这样的代码。

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,

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

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

如果只有这部分可以修改,我可以制作更灵活的代码。

PS。

我会解决我的问题。

string className = "ShellSection";或者其他的东西

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

typeof(className).GetMethod(pMethodName);

我在问上面的可能性和操作方法。

【问题讨论】:

标签: c# casting typeof


【解决方案1】:

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

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

而不是

typeof(ShellSection).GetMethod(pMethodName);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    相关资源
    最近更新 更多