Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / October 2004

Tip: Looking for answers? Try searching our database.

Delegates in .NET vs. runtime managed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George - 20 Oct 2004 17:17 GMT
Does anyone know what is behind the runtime managed ".ctor" and "Invoke"
methods of a delegate type?

What is performed in these methods whose bodies are provided by the runtime?

Any idea?

Thanks. George
Robert Jordan - 20 Oct 2004 23:49 GMT
> Does anyone know what is behind the runtime managed ".ctor" and "Invoke"
> methods of a delegate type?
>
> What is performed in these methods whose bodies are provided by the runtime?

I'm just guessing:

- "Invoke" does an "early-bind" (optimized) call of the delegate
- the constructor is preparing something for "Invoke"

bye
Rob
George - 21 Oct 2004 08:25 GMT
Dear Robert,

Thankx for the answer.

> I'm just guessing:
>
> - "Invoke" does an "early-bind" (optimized) call of the delegate
> - the constructor is preparing something for "Invoke"

I would be interested more in detail. What do you mean by "(optimized) call
of the delegate"?
What exactly is preparing the constructor?

many thanks again!
George
Robert Jordan - 21 Oct 2004 11:06 GMT
Hi George,

> Thankx for the answer.

You're welcome!

>>I'm just guessing:
>>
[quoted text clipped - 4 lines]
> of the delegate"?
> What exactly is preparing the constructor?

I'm *still* guessing ;-)

Delegates are (special) classes that derive from MulticastDelegate.
When you define a new delegate type you don't need (or even cannot?)
to provide an implementation because the runtime already provides
a common optimized implementation for all delegate types. That's why
all methods are to be declared "runtime managed" with an empty body.

"runtime managed" methods are black boxes. you don't need to
know what happens inside them, even when you're programming
lowlevel IL.

bye
Rob
Robert Jordan - 21 Oct 2004 11:29 GMT
Hi George,

I took a look an the IL of the following code:

delegate void MyEventHandler(object sender, EventArgs e);

The compiler generates a class derived from MulticastDelagate
consisting of these "runtime managed" (thus empty) methods:

.ctor
Invoke
BeginInvoke
EndInvoke

However, you *can* provide an CIL implementation too.
I changed "runtime managed" to "cil managed" and
it works:

  .method public hidebysig virtual instance void
          Invoke(object sender,
                 class [mscorlib]System.EventArgs e) cil managed
  {
    ldstr      "test"
    call       void [mscorlib]System.Console::WriteLine(string)
    ret
  }

So if you don't like the "runtime managed" black boxes
you still can roll your own delegate implementation, though
probably a bad idea.

bye
Rob
Mattias Sj?gren - 21 Oct 2004 16:20 GMT
Robert,

>However, you *can* provide an CIL implementation too.
>I changed "runtime managed" to "cil managed" and
>it works:

But is the result verifiable (test with PEVerify)? It certainly
violates section 13.6 in partition II of the ECMA specification.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Robert Jordan - 21 Oct 2004 16:45 GMT
Hi Mattias,

>>However, you *can* provide an CIL implementation too.
>>I changed "runtime managed" to "cil managed" and
>>it works:
>
> But is the result verifiable (test with PEVerify)? It certainly
> violates section 13.6 in partition II of the ECMA specification.

No, it doesn't pass the tests. Mono completely ignores the hack.

bye
Rob
George - 21 Oct 2004 17:24 GMT
Mattias,

> But is the result verifiable (test with PEVerify)? It certainly
> violates section 13.6 in partition II of the ECMA specification.

You are definitely right. Of course it violates the Spec.

Do you have any idea what is behind these runtime managed methods of a
delegate class?

Thank you. George
George - 21 Oct 2004 11:47 GMT
Hi Robert,

> When you define a new delegate type you don't need (or even cannot?)

it's "cannot" :))

> "runtime managed" methods are black boxes. you don't need to
> know what happens inside them, even when you're programming
> lowlevel IL.

exactly. this is the point of my question :) I want to know what is inside
these blackboxes (runtime managed). For example, I think that the .cctor sets
the two private fields "_target" and "_methodPtr". What else it does?

How it's the invocation with Invoke optimized?  

All the best, George
Robert Jordan - 21 Oct 2004 12:40 GMT
Hi George,

>>When you define a new delegate type you don't need (or even cannot?)
>
> it's "cannot" :))

Ok :-)

>>"runtime managed" methods are black boxes. you don't need to
>>know what happens inside them, even when you're programming
[quoted text clipped - 3 lines]
> these blackboxes (runtime managed). For example, I think that the .cctor sets
> the two private fields "_target" and "_methodPtr". What else it does?

Hmm, Reflector is definitely able to disassemble the System.Delegate
.ctor. No magic inside  apart from the InternalCalls :-)

You may take a look at Rotor (the shared source .NET from MS)
or at Mono (OpenSource).

Out of curiosity, what are you trying to achieve?

bye
Rob
George - 21 Oct 2004 12:53 GMT
Thank you Robert.

> Out of curiosity, what are you trying to achieve?

I want to (formally) model these delegates methods.
Bye. George
Mattias Sj?gren - 21 Oct 2004 16:31 GMT
George,

>I want to know what is inside these blackboxes (runtime managed).

Well that's implementation dependant and can vary from one CLI
implementation to another. For the CLR, see comdelegate.cpp/.h at

http://sharedsourcecli.sscli.net/source/browse/sharedsourcecli/clr/src/vm/

You can probably find similar code in the Mono source.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.