English 中文(简体)
Explain syntax of delegation in Occupation c#
原标题:Explain syntax of delegate invocation c#

http://c-sharp-programming.blogspot.com/2008/07/ Cross-thread-operation-not-valid.html” rel=“nofollow” http://c-sharp-programming.blogspot.com/2008/07/ cross-thread-operation-not-valid.html。

我基本上可以看到所发生的事情,但这一行的辛迪加具体地说:

label1.Invoke(del, new object[] { newText });

我感到困惑。 是否有人解释? 当只有一个参数时,我们为什么要为代表使用一个新的目标阵列。

<>未来代码:

delegate void updateLabelTextDelegate(string newText);
private void updateLabelText(string newText)
{
 if (label1.InvokeRequired)
 {
     // this is worker thread
     updateLabelTextDelegate del = new updateLabelTextDelegate(updateLabelText);
     label1.Invoke(del, new object[] { newText });
 }
 else
 {
     // this is UI thread
     label1.Text = newText;
 }
}
最佳回答

TL;DR:

<代码>Control. Invoke <>code> is calls DynamicInvoke on You delegation which take an Object range of criteria to work with any delegation category.

页: 1

The keyword delegate in C# in analagous to specifying a type of function pointer. You can use that type to pass methods of a specific signature. In your example, the signature is for a method that takes 1 arg (a string) and returns nothing (void). The method updateLabelText matches that sig. The line:

updateLabelTextDelegate del = new updateLabelTextDelegate(updateLabelText);

Is just a full-text way of saying:

updateLabelTextDelegate del = updateLabelText;

Then, you can pass your variable del, which is now a pointer to the method updateLabelText to the Control.Invoke method.

label1.Invoke(del, new object[] { newText });

由于<代码>第 正在<条码>中使用。 Invoke signature, 你甚至不必明确将其称作object[]

label1.Invoke(del, newText);

<代码>Invoke包含一系列物体,作为所指派代表的先驱。 (根据您的更新方法,可以发挥杠杆作用,不断阅读) 。 yourself:

del(newText);

基本情况与:

updateLabelText(newText);

在<代码>中。 Invoke, they are打上了del, but it don t have/strong>,以了解由于某些助手方法,这种增益会增加多少。 你会发现这样的情况:

EDIT I did some deep digging for science, the invocation internally is more like:

del.DynamicInvoke(args);

Where args is an object[]. For more info on things you can do with your delegate variable (which is of type Delegate), read more here.

问题回答

If you look at the method signature of Control.Invoke, you ll see it takes params Object[] args. You can either pass object[] args or a single argument.

标语阵列已传至<代码>Invoke。 在此情况下,updateLabelTextDelegate 采用单一<条码>载参数,因此阵列中的单一要素。

事实上,不需要明确建立阵列,

label1.Invoke(del, newText)

同样有效。

首先,值得注意的是,该代表的密码是<>Invoke ——该代表的电话号码是Invoke上的<>。 现在请看Control。 此处使用的是

public Object Invoke(
    Delegate method,
    params Object[] args
)

如果该方法使用某一具体代表类型,该代表可使用适当的参数类型。 your> 如果你的代表只需要一个参数,但希望通过一个<编码>。 行动调整;加强、加强、强化和强化;——采用上述非常一般的做法,我们可以做到:

control.Invoke(someDelegate, new object[] { "foo", "bar", 10 });

因此,答案是,<条码>目标[]是提供一般性的,因为代表的类型也普遍存在。 缩略语MethodInfo。 Invoke <>code> - 若在汇编时不知道有多少参数,则具备<代码>目标[的价值是允许各种情况的最佳途径。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...