如何使用c#从字符串调用方法?
我创建了一个类库,它有4个类,每个类有1个方法。 第一个类是我的主类,在我的第一个类中,我有一个名为calltoaction的字符串,在这个字符串中,我将动态地得到下面列表中的一个如何使用c#从字符串调用方法?,c#,C#,我创建了一个类库,它有4个类,每个类有1个方法。 第一个类是我的主类,在我的第一个类中,我有一个名为calltoaction的字符串,在这个字符串中,我将动态地得到下面列表中的一个 类别2.方法2() 类别3.方法3() 类别4.方法4() 现在我想从字符串“calltoaction”执行“class2.method2” 例如: class Class1 { public void method1() { string calltoaction = "Class2
class Class1
{
public void method1()
{
string calltoaction = "Class2.Method2()";
}
}
如何从字符串执行“Class2.Method”?我想一种低技术的方法是使用如下的switch语句:
using System;
namespace ConsoleApplication24
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Which method would you like to run?");
RunMyMethod(Console.ReadLine());
}
private static void RunMyMethod(string p)
{
switch (p)
{
case "MethodOne();":
MethodOne();
break;
case "MethodTwo();":
MethodTwo();
break;
case "MethodThree();":
MethodThree();
break;
}
}
private static void MethodThree()
{
//Do Stuff
}
private static void MethodTwo()
{
//Do Stuff
}
private static void MethodOne()
{
//Do Stuff
}
}
}
我想一种低技术的方法是使用如下的switch语句:
using System;
namespace ConsoleApplication24
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Which method would you like to run?");
RunMyMethod(Console.ReadLine());
}
private static void RunMyMethod(string p)
{
switch (p)
{
case "MethodOne();":
MethodOne();
break;
case "MethodTwo();":
MethodTwo();
break;
case "MethodThree();":
MethodThree();
break;
}
}
private static void MethodThree()
{
//Do Stuff
}
private static void MethodTwo()
{
//Do Stuff
}
private static void MethodOne()
{
//Do Stuff
}
}
}
我不完全确定你想要完成什么,但我相信你可以用更好的方式来完成。基本上,如果我正确理解了您的问题,那么调用此函数将返回您希望执行的类和方法的名称 如果是这样的话,我会无限期地删除整个“字符串”的内容,然后开始查看代理 考虑这一点:
public class Class2
{
public static void Method2() { }
} // eo class 2
public class Class3
{
public static void Method3() { }
} // eo class 3
public class Class4
{
public static void Method4() { }
} // eo class 4
现在我们来到我们的主课
public class MainClass
{
private delegate void MethodDelegate();
private List<MethodDelegate> delegates_ = new List<MethodDelegate>();
// ctor
public MainClass()
{
delegates_.Add(Class2.Method2);
delegates_.Add(Class3.Method3);
delegates_.Add(Class4.Method4);
}
// Call a method
public void Method1()
{
// decide what you want to call:
delegates_[0].Invoke(); // "Class2.Method2"
} // eo Method1
} // eo class Main
public类MainClass
{
私有委托void MethodDelegate();
私有列表委托\=新列表();
//执行器
公共类()
{
代理添加(类2.Method2);
代理添加(类3.Method3);
代表添加(类别4.方法4);
}
//调用一个方法
公共无效方法1()
{
//决定你想叫什么:
委托[0]。调用();/“Class2.Method2”
}//eo方法1
}//eo类主
我不完全确定您想要完成什么,但我相信可以以更好的方式完成。基本上,如果我正确理解了您的问题,那么调用此函数将返回您希望执行的类和方法的名称
如果是这样的话,我会无限期地删除整个“字符串”的内容,然后开始查看代理
考虑这一点:
public class Class2
{
public static void Method2() { }
} // eo class 2
public class Class3
{
public static void Method3() { }
} // eo class 3
public class Class4
{
public static void Method4() { }
} // eo class 4
现在我们来到我们的主课
public class MainClass
{
private delegate void MethodDelegate();
private List<MethodDelegate> delegates_ = new List<MethodDelegate>();
// ctor
public MainClass()
{
delegates_.Add(Class2.Method2);
delegates_.Add(Class3.Method3);
delegates_.Add(Class4.Method4);
}
// Call a method
public void Method1()
{
// decide what you want to call:
delegates_[0].Invoke(); // "Class2.Method2"
} // eo Method1
} // eo class Main
public类MainClass
{
私有委托void MethodDelegate();
私有列表委托\=新列表();
//执行器
公共类()
{
代理添加(类2.Method2);
代理添加(类3.Method3);
代表添加(类别4.方法4);
}
//调用一个方法
公共无效方法1()
{
//决定你想叫什么:
委托[0]。调用();/“Class2.Method2”
}//eo方法1
}//eo类主
使用操作
而不是字符串(假设不需要返回值。如果需要-使用函数
):
这是一个如何使用它的想法:
public Form1()
{
InitializeComponent();
Action<string> calltoaction;
calltoaction = Doit;
calltoaction("MyText1");
calltoaction = Doit2;
calltoaction("MyText2");
}
void Doit(string s)
{ Text = s; }
void Doit2(string s)
{ textBox1.Text = s; }
public Form1()
{
初始化组件();
行动号召行动;
calltoaction=Doit;
calltoaction(“MyText1”);
calltoaction=Doit2;
调用操作(“MyText2”);
}
void Doit(字符串s)
{Text=s;}
void Doit2(字符串s)
{textBox1.Text=s;}
使用操作
而不是字符串(假设不需要返回值。如果需要-使用函数
):
这是一个如何使用它的想法:
public Form1()
{
InitializeComponent();
Action<string> calltoaction;
calltoaction = Doit;
calltoaction("MyText1");
calltoaction = Doit2;
calltoaction("MyText2");
}
void Doit(string s)
{ Text = s; }
void Doit2(string s)
{ textBox1.Text = s; }
public Form1()
{
初始化组件();
行动号召行动;
calltoaction=Doit;
calltoaction(“MyText1”);
calltoaction=Doit2;
调用操作(“MyText2”);
}
void Doit(字符串s)
{Text=s;}
void Doit2(字符串s)
{textBox1.Text=s;}
你能解释得更清楚一点吗?如果您只想调用method1()
来调用Class2.Method2()。你为什么要这么做?很可能有更好的方法。您正在寻找的是反射。关于这个主题已经有很多教程了。也许你可以先读一下这些。如果你想在不使用switch语句预定义的情况下调用任何方法,你需要使用反射。然后,您可以查找与您传入的名称匹配的方法,然后调用它。这是一个非常奇怪的请求!仍然有可能这只是一个假的场景来演示一个概念。无论如何,c#依赖于面向对象的范例,并拥有一个称为多态性的概念。这意味着您有一个可以实现多个类的接口,调用方不需要耦合到特定的类。答案中有人建议您也有机会使用代理。以这种方式使用反射被称为滥用。你能更清楚地解释一下吗?如果您只想调用method1()
来调用Class2.Method2()。你为什么要这么做?很可能有更好的方法。您正在寻找的是反射。关于这个主题已经有很多教程了。也许你可以先读一下这些。如果你想在不使用switch语句预定义的情况下调用任何方法,你需要使用反射。然后,您可以查找与您传入的名称匹配的方法,然后调用它。这是一个非常奇怪的请求!仍然有可能这只是一个假的场景来演示一个概念。无论如何,c#依赖于面向对象的范例,并拥有一个称为多态性的概念。这意味着您有一个可以实现多个类的接口,调用方不需要耦合到特定的类。答案中有人建议您也有机会使用代理。使用反射的方式称为滥用。我将在字符串中一次性获得一个方法,我希望立即执行该方法。比如对于Ex:String calltoaction=“Class2.Method2”,一旦将值分配给字符串,我想立即调用该方法。您可能需要详细说明您想要做什么。委托可以像任何其他变量类型一样返回、传递和使用。然后只需调用Invoke()
即可执行它。我仍然不明白为什么您可能需要字符串报告