• C# 将字符串转为函数名


    class Program

    {

      static void Main(string[] args)

      {

        Type t = typeof(Program);//参数为所要使用的函数所在的类的类名。

        MethodInfo mt = t.GetMethod("foo");

        if (mt == null)

        {

          Console.WriteLine("没有获取到相应的函数!!");

        }

        else

        {

          string str = (string)mt.Invoke(null,new object[] { "1234567890" });

         }

        Console.ReadKey();

       }

      private static string foo(string abc)

      {

        return abc;

      }

    }

    来源:https://blog.csdn.net/realDonaldTrump/article/details/79664926

    附【仅参考用】:

    Type t = typeof(controlServerBll);//括号中的为所要使用的函数所在的类的类名。
    Dictionary<string, object> dataDic = JsonUtil.ToDictionary(data);
    string controlFunc = "";
    if (dataDic["controlType"].ToString() == "1")
    {
    controlFunc = "OpenServer";
    }
    else if (dataDic["controlType"].ToString() == "2")
    {
    controlFunc = "StopServer";
    }
    else if (dataDic["controlType"].ToString() == "3")
    {
    controlFunc = "RestartServer";
    }
    MethodInfo mt = t.GetMethod(controlFunc, BindingFlags.Default);
    responseData = (string)mt.Invoke(null, new object[] { serverName });

    LogHelper<ClassName>.Debug(responseData);

  • 相关阅读:
    new delate he typedef的含义
    Importing the multiarray numpy extension module failed
    QT socket相关
    CMake的一些使用
    CMake undefined reference to `QTcpServer::QTcpServer(QObject*)'的解决
    MFC操作excel
    dsview
    phyton 相关学习
    面试相关
    远程连接
  • 原文地址:https://www.cnblogs.com/wa502/p/11603373.html
Copyright © 2020-2023  润新知