本文介绍了C#泛型2.0:如何创建一个Action对象零参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我使用VS2005和C#2.0。

First of all, I'm using VS2005 and C# 2.0.

我试图从SelectedIndexChanged事件内设置一个组合框Text属性。从另一个线程在这里计算器这个提议做了以下方式:

I'm trying to set a combobox' Text property from inside the SelectedIndexChanged event. From another thread here on StackOverflow this was proposed done the following way:

BeginInvoke(new Action(() => someCombobox.Text = "x" ));

现在,首先这将返回一个编译器错误我。我相信这是因为Action对象在这两个语言规范的行为不同。在C#2.0中,Action对象似乎需要在< T>在所有声明结构。也许我错了,但我想有一个澄清

Now, first of all this returns a compiler error for me. I believe that is because the Action object behaves differently in the two language specifications. In C# 2.0, the Action object seems to need the <T> structure in all declarations. Maybe I'm wrong, but I'd like to have that clarified.

这是什么工作如下:

BeginInvoke(new Action<string>( delegate { someCombobox.Text = "x"; }), new object[] { "" });



然而,它只是似乎很奇怪,我认为我的有无以的定义与一类参数Action对象(尤其是因为我不打算传递任何参数)!不知怎的,删除此参数也将使空新的对象[]过时了,这就是我想要的。

However, it just seems very weird to me that I have to define the Action object with a type parameter (especially since I'm not intending to pass any parameters)! Somehow removing this parameter would also make the empty new object[] obsolete, which is what I want.

谁能帮我简化以上的电话吗?

Can anyone help me simplify the above call?

最后,它是保证的BeginInvoke会的SelectedIndexChanged后完成,因此更新正确的文本组合框Text属性?

Finally, is it guaranteed that BeginInvoke will finish after the SelectedIndexChanged and thus update the combobox' Text property with the correct text?

我会很感激学习这些问题的答案。

I'd really appreciate to learn the answers to these questions.

推荐答案

我不认为行动不带参数是可用的。 NET 2.0
无后顾之忧 - 只是用不同的预定义的委托类型。
MethodInvoker应该做的工作(不带参数无效的方法)

I don't think Action without parameters is available in .NET 2.0No worries - just use a different predefined delegate type.MethodInvoker should do the job (void method with no parameters).

另外,BeginInvoke的有2个重载 - 一个接受委托,和一个带有一个委托和对象的数组。

Also, BeginInvoke has 2 overloads - one that takes a delegate, and one that takes a delegate and array of objects.

BeginInvoke(new MethodInvoker(delegate()
{
    someCombobox.Text = "x";
}));

这篇关于C#泛型2.0:如何创建一个Action对象零参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:16