Re: Generic Delegate

Javier Martínez Álvarez <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <[email protected]>
Ok, thanks by your great response.



-----Mensaje original-----
De: Discussion relating to the specifics of the C# and Managed C++ languages
[mailto:[email protected]] En nombre de Ian Griffiths
Enviado el: viernes, 11 de agosto de 2006 0:29
Para: [email protected]
Asunto: Re: [DOTNET-CX] Generic Delegate

I wrote:
> You can specify any delegate type you like in there.

...I should have qualified that. Any delegate type you like so long as it
has a void return type.

Also, speaking of possible solutions, I guess you could do something like
define a delegate that takes a params style object[] array, and then use
lightweight code gen to build shims between that and real methods... But
there's nothing built in that does this for you today AFAIK.  How much
effort are you prepared to put in to get this feature?..  Maybe we'll see
this kind of thing emerge as the fruits of Microsoft's investment in dynamic
language support, but today there's no intrinsic support for this.


Ian

-----Original Message-----
From: Discussion relating to the specifics of the C# and Managed C++
languages [mailto:[email protected]] On Behalf Of Ian Griffiths
Sent: 10 August 2006 23:25
To: [email protected]
Subject: Re: [DOTNET-CX] Generic Delegate

Javier Martínez Álvarez wrote:
> Why isn't possible to use the following sentence?
>
> System.Delegate x = delegate{};

Because the compiler doesn't know what kind of delegate type to create.

System.Delegate is abstract, so it can't create one of those - the delegate
instance has to have a concrete type. (Besides, System.Delegate doesn't
actually define an Invoke method, and consequently there is no signature
associated with it. So the compiler doesn't know what signature to give the
function you're asking it to generate.)

You can do any of the following though:

  System.Delegate x = (EventHandler) delegate{};
  System.Delegate x = (MouseEventHandler) delegate{};
  System.Delegate x = (AsyncCallback) delegate{};  


...and many many more. You can specify any delegate type you like in there.
The fact that any delegate type works illustrates the problem - the C#
compiler doesn't know which of these to create if you don't tell it.

From the title you've given this message, I'm guessing that perhaps you'd
like to be able to have some construct that lets you refer to any type of
method at all, rather than having to choose a specific delegate type.

That's essentially a late bound kind of behavior, and delegates are not
about late binding. However, it's possible to do this with reflection. All
you need is a MethodInfo and an object reference, and then you can use
MethodInfo.Invoke to do the late bound invocation. You could wrap that
functionality up into a class and you've now got your own generic late-bound
equivalent to delegates.

The Delegate class does offer a late bound invocation facility with
DynamicInvoke. However, that still requires a delegate with a concrete type.
The delegate itself is effectively early-bound to the target method, and
DynamicInvoke simply provides a late-bound wrapper around that.

This is a bit of an annoying limitation for certain styles of programming,
particularly if you're using a language that does offer exactly this idea of
a general purpose late-bound method reference. If you look at languages that
have this and have been adapted to the CLR, one solution that seems to be
emerging (and which might be formally incorporated into the .NET class
libraries in the LINQ time frame, as LINQ also turns out to have a need for
this concept) is to use a series of delegate types looking something like
this:

  delegate T Func<T>();
  delegate T Func<T, A1>(A1 a1);
  delegate T Func<T, A1, A2>(A1 a1, A2 a2);
  delegate T Func<T, A1, A2, A3>(A1 a1, A2 a2, A3 a3);
  delegate T Func<T, A1, A2, A3, A4>(A1 a1, A2 a2, A3 a3, A4 a4); etc.

This then makes it possible to offer a facility that can take any method and
return a delegate that points to it. Although that's only half the story -
you'd still need language support for it. (And this is also presumes there's
a return type. If you're dealing with non-functional methods as well as
functions this adds complication...)

It's also not a very nice solution...  But it's hard to find a more elegant
solution that doesn't involve reflection. (Reflection lets you deal with
this much more cleanly, but has some significant performance issues.) 
--
Ian Griffiths

-----Original Message-----
From: Javier Martínez Álvarez
Sent: 09 August 2006 16:56

Hi

Why isn't possible to use the following sentence?

System.Delegate x = delegate{};

Thanks in advance

Javier

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.