A way for "this" (or some other name) to refer to the anonymous delegate
itself, not the containing class. So for example, inside my anonymous
method I want to call an Unregister() handler to unregister the delegate
from some dispatcher. I need to have a ref to the delegate. And there is
the challenge. I have no way to refer to myself inside the method unless I
also pass that information to the delegate. Like:
// Some anonoumous method.
(string s, delegate self)
{
// use s.
if ( isTrue )
UnRegister(self);
}
This would work, but junks up the delegate signatures.
--
William Stacey [C# MVP]
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
| William Stacey [C# MVP] <wi************@gmail.comwrote:
| It would be handy to be able to ref "this" from inside an AM such as:
| >
| (string s)
| {
| Console.Writeline(s);
| DoSomething(this);
| }
| >
| So treating am like a method of a class (which it is). Currently we have
no
| context to know so you have to pass that as state which seems kinda
| redundant. Naturally, "this" would need to be named something else.
Maybe
| "that"? ;/ Double-double this-this, double-double that-that,
double-this
| double-that, double-double this-that.
|
| I'm not sure I understand what you mean - you already *do* have access
| to "this":
|
| using System;
|
| delegate void SimpleDelegate();
|
| class Test
| {
| string name;
|
| Test(string name)
| {
| this.name = name;
| }
|
| public static void Main()
| {
| Test t = new Test("Jon");
| SimpleDelegate am = t.Foo();
| am();
| }
|
| SimpleDelegate Foo()
| {
| string s = "Hello";
| return delegate
| {
| Console.WriteLine (s);
| DoSomething(this);
| };
| }
|
| static void DoSomething(Test t)
| {
| Console.WriteLine (t.name);
| }
| }
|
| (I'm sure there's a general purpose parameterless void delegate
| somewhere, but I can't remember what it is. I originally used
| ThreadStarty, but thought it might confuse things.)
|
|
| My guess is that you're aware of the above and you're actually after
| something else, but it's not clear to me at the moment what that is...
|
| --
| Jon Skeet - <sk***@pobox.com>
|
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too