以字符串作为索引和函数指针或委托作为值的 SortedList?通过字符串调用函数?
我不熟悉委托和 lambda,但此刻,我必须做这两件事:
- 其中之一是:
我需要将函数传递给 SortedList。我不知道如何才能做到。
为什么我需要它,是:
private string Func1(string s) { return s + "1"; }
private string Func2(string s) { return s + "2"; }
private string Func3(string s) { return s + "3"; }
private void WhatEver()
{
SortedList<string, ???> list = new SortedList<string, ???>;
list.Add("Func1", Func1);
list.Add("Func2", Func2);
list.Add("Func3", Func3);
// And after that I have to pass values and get results from functions
// Like
Console.WriteLine(list["Func1"]("Test"));
Console.WriteLine(list["Func2"]("Test"));
Console.WriteLine(list["Func3"]("Test"));
// Output should be:
//
// Test1
// Test2
// Test3
}
- 第二:
是否可以用字符串调用函数?
例如:
假设我有三个文本框和一个函数:
tbFunction
tbArgument
tbResult
private string Test(string number)
{
int x = int.Parse(number);
return (x * x).ToString();
}
假设我在 tbFunction.Text 中有“Test”,在 tbArgument.Text 中有“2”,我如何将结果带到 tbResult.Text
I'm not familiar with delegates and lambdas but at this moment, I have to do these two things:
- One of them is:
I need to pass the function to SortedList. I don't know how it can be done.
Why I need it, is:
private string Func1(string s) { return s + "1"; }
private string Func2(string s) { return s + "2"; }
private string Func3(string s) { return s + "3"; }
private void WhatEver()
{
SortedList<string, ???> list = new SortedList<string, ???>;
list.Add("Func1", Func1);
list.Add("Func2", Func2);
list.Add("Func3", Func3);
// And after that I have to pass values and get results from functions
// Like
Console.WriteLine(list["Func1"]("Test"));
Console.WriteLine(list["Func2"]("Test"));
Console.WriteLine(list["Func3"]("Test"));
// Output should be:
//
// Test1
// Test2
// Test3
}
- Second:
Is it possible to call functions with strings?
Ex:
Let's say I've got three textboxes and a function:
tbFunction
tbArgument
tbResult
private string Test(string number)
{
int x = int.Parse(number);
return (x * x).ToString();
}
Let's say I've got "Test" in tbFunction.Text and "2" in tbArgument.Text, How Can I bring the result to tbResult.Text
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我花了一段时间才弄清楚你想要什么。因此,如果我正确理解您的意思,您需要:
简单的答案是没有内在的方法可以做到这一点。您可能可以使用反射来拼凑一些东西,但这会付出更多的努力而不是值得的。
因此,我们需要创建自己的调度表:
更进一步,用于调用它的名称(在字符串中)不需要与函数本身的名称有任何联系。该方法甚至不需要有名称:
It took a while to figure out what you wanted. So, if I follow you correctly, you want:
The simple answer is that there is no intrinsic means of doing that. You could probably cobble together something using Reflection, but it would be more efforts than it's worth.
So, we'll need to create our own dispatch table:
Carrying this further, the name (in the string) used to call it need not have any connection to the name of the function itself. The method doesn't even have to have a name:
这实际上是反射的工作,与 lambda 或委托无关。
您将需要执行类似的操作...
查看 System.Reflection 文档。
This is actually a job for reflection, has nothing to do with lambdas or delegates.
You will need to do something like...
Check around in the System.Reflection documentation.
我认为这正是您所要求的。
就我个人而言,我会使用 Dictinary,但这可能只是一个品味问题。
或者,您可以使用反射,这样您就不需要在列表中注册每个方法。
I think that this is just about what you are asking for.
Personally I would use Dictinary but that is probably just a matter of taste.
Alternatively you could use reflection, that way you would not need to register each method in a list.