c# 根据输入指定方法调用?
如何最好地允许 ac# 程序的输入来控制方法调用?例如:
假设我们有一个委托类型:
delegate void WriteMe();
和几个方法:
void PrintInt() { Console.WriteLine(10); }
void PrintString() { Console.WriteLine("Hello world."); }
并允许输入选择调用顺序:
public static WriteMe ProcessInvocationInput(int[] val) {
WriteMe d = null;
foreach (int i in val) {
switch (i) {
case 1: d += PrintInt; break;
case 2: d += PrintString; break;
}
}
}
以及调用这一切的代码:
static void Main(string args[]) {
int[] values = {1, 2, 3}; // Obviously this array could be filled
// from actual input (args, file, wherever)
WriteMe d = ProcessInvocationInput(values);
d();
}
我发布这个问题的原因是因为它实现起来似乎相当复杂这似乎是一个简单的想法。我知道实现此行为的另一种方法是使用反射 API,但这会更加复杂。
How would one best go about allowing the input of a c# program to control method invocation? For example:
Assume we have a delegate type:
delegate void WriteMe();
And a couple of methods:
void PrintInt() { Console.WriteLine(10); }
void PrintString() { Console.WriteLine("Hello world."); }
And allowing the input to select the invocation order:
public static WriteMe ProcessInvocationInput(int[] val) {
WriteMe d = null;
foreach (int i in val) {
switch (i) {
case 1: d += PrintInt; break;
case 2: d += PrintString; break;
}
}
}
And the code that calls it all:
static void Main(string args[]) {
int[] values = {1, 2, 3}; // Obviously this array could be filled
// from actual input (args, file, wherever)
WriteMe d = ProcessInvocationInput(values);
d();
}
The reason I'm posting this question is because it seems rather complex to implement what seems like a simple idea. I know another way to accomplish this behavior is with the reflection API, but that would be even more convoluted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您为您可以执行的操作创建值字典
那么您可以循环和处理
If you create a dictionary of values to Actions you can do
Then you can just loop and process
这实际上取决于您想要涵盖的范围。对于简单的情况,您可以使用开关(我建议使用枚举来明确说明):
但是如果您正在编写 shell,则需要更强大的东西,例如某种
IExecutableCommand
接口由各个类实施。您将必须实现一些解析器来处理多个调用请求和/或处理更复杂的参数。
如果您想使用反射,请务必验证您的输入!这可以通过仅执行具有自定义属性的方法来完成。
使用此属性过滤掉方法非常简单:
That really depends on the scope you're trying to cover. For simple cases you could use a switch (I'd suggest and enum to make it clear):
But if you're writing a shell, than you need something more robust, like some sort of
IExecutableCommand
interface implemented by various classes.You will have to implement some parser to handle multiple invocation requests and/or handle more complex arguments.
If you want to use Reflection, be sure to validate your input! That could be done by only executing methods with a custom attribute on them.
Filtering out methods with this attribute is easy enough: