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;
}
}
来源:blog.csdn/realDonaldTrump/article/details/79664926
附【仅参考⽤】:
Type t = typeof(controlServerBll);//括号中的为所要使⽤的函数所在的类的类名。
Dictionary<string, object> dataDic = JsonUtil.ToDictionary(data);
string controlFunc = "";
if (dataDic["controlType"].ToString() == "1")
{writeline函数
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);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论