如何使用 Action<Action<object>, object> ?这是正确的方法吗?
how can use Action<Action<object>, object> ? Is this the right way?
为每个对象创建一个方法很麻烦。
同类型的每个方法只用一个动作不就可以吗?
Action<Action<object>, object> action;
private void init_Shown(object sender, EventArgs e)
{
// how write code?
action = (a, o) =>
{
//Action<object>
a(o);
};
}
// this method used by another thread
private void test(object obj)
{
// i want this like. and use other methods.
this.Invoke(action, listbox.Items.Add, obj);
}
这是代码
private void init_Shown(object sender, EventArgs e)
{
listboxAdd = (obj) =>
{
listbox.Items.Add(obj);
};
}
// this method used by another thread
private void test(object obj)
{
this.Invoke(listboxAdd, obj);
}
listbox.Items.Add
与Action<object>
具有相同的签名,因此编译器可以在需要Action<object>
时直接使用它。这应该有效:
private void test(object obj)
{
this.Invoke(listbox.Items.Add, obj);
}
为每个对象创建一个方法很麻烦。
同类型的每个方法只用一个动作不就可以吗?
Action<Action<object>, object> action;
private void init_Shown(object sender, EventArgs e)
{
// how write code?
action = (a, o) =>
{
//Action<object>
a(o);
};
}
// this method used by another thread
private void test(object obj)
{
// i want this like. and use other methods.
this.Invoke(action, listbox.Items.Add, obj);
}
这是代码
private void init_Shown(object sender, EventArgs e)
{
listboxAdd = (obj) =>
{
listbox.Items.Add(obj);
};
}
// this method used by another thread
private void test(object obj)
{
this.Invoke(listboxAdd, obj);
}
listbox.Items.Add
与Action<object>
具有相同的签名,因此编译器可以在需要Action<object>
时直接使用它。这应该有效:
private void test(object obj)
{
this.Invoke(listbox.Items.Add, obj);
}