Jacek Wojcik
Jacek Wojcik

Reputation: 1253

Func delegate used as a parameter

I am trying to understand what the code below actually does.

We have Submit method that returns void and takes two arguments:

Then we call Submit method with arguments.

This part of code I do not understand.

void Submit(Delegate d, params object[] arguments)
{
    ServiceQueue.Get.Submit(d, arguments);
}

Submit(new Func<BusinessMetadataQueryDataContract,
            AsyncCallback,
            object,
            IAsyncResult>(
                this.Channel.BeginBusinessMetadataGet),
                contract,
                new AsyncCallback(
                    (iar) =>
                    {
                        BusinessMetadataDataContract outContract = null;
                        Action<Exception, OpusReturnType> response =
                            (e, ort) =>
                            {
                                SilverlightClient.UIThread.Run(() =>
                                {
                                    this.BusinessMetadataGetActionCompleted(this,
                                        new ActionCompletedEventArgs<BusinessMetadataDataContract>(
                                            ort,
                                            outContract,
                                            e,
                                            false,
                                            asyncState));
                                });
                            };

                        try
                        {
                            response(null,
                                this.Channel.EndBusinessMetadataGet(
                                    out outContract,
                                    iar));
                        }
                        catch (Exception e)
                        {
                            response(e,
                                new OpusReturnType());
                        }
                    }),
                asyncState);

Func method takes three arguments and returns value.

So we have arguments of type:

and we have return type:

Then we have: (this.Channel.BeginBusinessMetadataGet),

I do not understand it. What is it doing here? I was expecting an opening bracket ( and a first parameter of type BusinessMetadataQueryDataContract, instead I get (this.Channel.BeginBusinessMetadataGet) and expected parameter is positioned on the second position.

I must be missing something here.

Any help? Thank you!

Upvotes: 0

Views: 136

Answers (1)

H&#229;kan Fahlstedt
H&#229;kan Fahlstedt

Reputation: 2095

this.Channel.BeginBusinessMetadataGet is the actual delegate, which have the signature that you describe. So the first argument (Delegate d) is this:

new Func<BusinessMetadataQueryDataContract,
            AsyncCallback,
            object,
            IAsyncResult>(
   this.Channel.BeginBusinessMetadataGet)

this creates a delegate from the method BeginBusinessMetadataGet. After this are all the parameters that will be used by the delegate.

Upvotes: 1

Related Questions