以字符串作为索引和函数指针或委托作为值的 SortedList?通过字符串调用函数?

发布于 2024-12-01 09:29:34 字数 1068 浏览 1 评论 0原文

我不熟悉委托和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恬淡成诗 2024-12-08 09:29:34

我花了一段时间才弄清楚你想要什么。因此,如果我正确理解您的意思,您需要:

  • 给定字符串变量中的方法名称,调用比方法。

简单的答案是没有内在的方法可以做到这一点。您可能可以使用反射来拼凑一些东西,但这会付出更多的努力而不是值得的。

因此,我们需要创建自己的调度表:

// class member
var jumptbl = new SortedList<string, Func<string, string> >();

 // :  (in ctor)
jumptbl.Add("Test", Test);

// :  (I'm guessing this is in a Click handler)
tbResult.Text = jumptbl[tbFunction.Text](tbArgument.Text)

更进一步,用于调用它的名称(在字符串中)不需要与函数本身的名称有任何联系。该方法甚至不需要有名称:

jumptbl.Add("Func1", s=> s + "1");
jumptbl.Add("Func2", s=> s + "2");
jumptbl.Add("Func3", s=> s + "3");

It took a while to figure out what you wanted. So, if I follow you correctly, you want:

  • Given the name of a method in a string variable, call than method.

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:

// class member
var jumptbl = new SortedList<string, Func<string, string> >();

 // :  (in ctor)
jumptbl.Add("Test", Test);

// :  (I'm guessing this is in a Click handler)
tbResult.Text = jumptbl[tbFunction.Text](tbArgument.Text)

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:

jumptbl.Add("Func1", s=> s + "1");
jumptbl.Add("Func2", s=> s + "2");
jumptbl.Add("Func3", s=> s + "3");
漫雪独思 2024-12-08 09:29:34

这实际上是反射的工作,与 lambda 或委托无关。

您将需要执行类似的操作...

MethodInfo method = typeof(ClassWithMethods).GetMethod(tbFunction.Text);
ClassWithMethods obj = new ClassWithMethods();
string result = method.Invoke(obj, new[] {tbArgument.Text});
tbResult.Text = result;

查看 System.Reflection 文档。

This is actually a job for reflection, has nothing to do with lambdas or delegates.

You will need to do something like...

MethodInfo method = typeof(ClassWithMethods).GetMethod(tbFunction.Text);
ClassWithMethods obj = new ClassWithMethods();
string result = method.Invoke(obj, new[] {tbArgument.Text});
tbResult.Text = result;

Check around in the System.Reflection documentation.

凡尘雨 2024-12-08 09:29:34
public delegate string YourDelegate(string number);
SortedList<string, YourDelegate> methods = new SortedList<string, YourDelegate>();

// Add more methods here
methods .Add("Test", Test);
...

public string CallMethod(string methodName, string number)
{
    YourDelegate method;
    if (methods.TryGetValue(methodName, out method))
        return method(number);
    else
        return "Unknown method";
}
...

public void button1_Click(object sender, EventArgs e)
{
    tbResult.Text = CallMethod(tbFunction.Text, tbArgument.Text);
}

我认为这正是您所要求的。
就我个人而言,我会使用 Dictinary,但这可能只是一个品味问题。

或者,您可以使用反射,这样您就不需要在列表中注册每个方法。

public delegate string YourDelegate(string number);
SortedList<string, YourDelegate> methods = new SortedList<string, YourDelegate>();

// Add more methods here
methods .Add("Test", Test);
...

public string CallMethod(string methodName, string number)
{
    YourDelegate method;
    if (methods.TryGetValue(methodName, out method))
        return method(number);
    else
        return "Unknown method";
}
...

public void button1_Click(object sender, EventArgs e)
{
    tbResult.Text = CallMethod(tbFunction.Text, tbArgument.Text);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文