Jon,
> So.. the questions are:
> 1. Is it possible to define a new keyword and the code it should emit?
No, This is the language that the compiler understands. You cannot extend
the language, unless you build your own compiler and probably call your
language something else. I believe since C# is standardized language if you
altered you cannot call it C# anymore.
I read some column in some of the magazines about a new Microsof project
code name "Phoenix" (
http://research.microsoft.com/phoenix/). If one day
this gets released it could be possible to do something like this
attributing your delegates.
I don't know way you want to alter the language. I think is as good as it
is. C# is OO language and .NET is OO framework, why you don't just create a
class that will take care of this. The class should be pretty simple. Yes it
won't be like part of the language, but this is shouldn't be a big deal.
Here is some sample that I jotted down:
public sealed class SyncDelegate<T>
{
private ISynchronizeInvoke syncObject;
private Delegate internalDelegate;
private SyncDelegate(ISynchronizeInvoke sync, Delegate
handlerDelegate)
{
syncObject = sync;
this.internalDelegate = handlerDelegate;
}
private void Handler(object sender, T e)
{
// Marshaling the event handler call in the UI thread.
this.syncObject.Invoke(internalDelegate, new object[] { sender,
e });
}public sealed class SyncDelegate<T>
{
private ISynchronizeInvoke syncObject;
private Delegate internalDelegate;
private SyncDelegate(ISynchronizeInvoke sync, Delegate
handlerDelegate)
{
syncObject = sync;
this.internalDelegate = handlerDelegate;
}
private void Handler(object sender, T e)
{
// Marshaling the event handler call in the UI thread.
this.syncObject.Invoke(internalDelegate, new object[] { sender,
e });
}
public static Delegate Create(ISynchronizeInvoke sync, Delegate
handlerDelegate)
{
SyncDelegate<T> syncDel = new SyncDelegate<T>(sync,
handlerDelegate);
return Delegate.CreateDelegate(handlerDelegate.GetType(),
syncDel, "Handler");
}
}
public static Delegate Create(ISynchronizeInvoke sync, Delegate
handlerDelegate)
{
SyncDelegate<T> syncDel = new SyncDelegate<T>(sync,
handlerDelegate);
return Delegate.CreateDelegate(handlerDelegate.GetType(),
syncDel, "Handler");
}
}
and here is how you use it:
Assuming that you want to handle an event declared as EventHandler and you
want to marshal the call to the UI thread that created *form*
someObj.MyEvent += (EventHandler)SyncDelegate<EventArgs>.Create(form, new
EventHandler(DoMyEvent));
Yes it is not as beautiful as couple of new keywords, but it still works and
is language independent.
> 3. Is the C# compiler's support for the delegate keyword hard-coded or
> part of a more customisable framework?
It is not hard-coded per se it is language keyword and as such is well
defined, documented and standardized.
..
> 4. Does anyone know how the runtime resolves methods of
> compiler-supplied delegate classes (specifically the Invoke and
> BeginInvoke methods which appear 'stubbed-out' in the > generated IL)?
I suppose you are talking Control.Invoke and BeginInvoke. They both use the
message queue and window messages to marshal the call.
The same technique is used with ActiveX controls created in STA threads.
However, I find interesting that you brought up this question. We are all
reading about the new exiting technologies that Microsoft talk about such as
LINQ, CCR (Concurrency and Coordination Runtime). Everything sounds really
good and powerful no doubt bout it. Very often they talk about integrating
this into the language, by introducing new keywords and language constructs
and we should probably be anxious to have these new languages that will make
our programming life easier.
The other day I read Charles Petzold's speech "Does Visual Studio Rot the
Mind?". There he gives some stats about .Net 2.0 having only in the system
assemblies 5,000 public classes that include over 45,000 public methods and
15,000 public properties. His idea is that there are no more programmers
that know everything and the programmers now specialize in some part of the
framework. This is normal for a framework or a platform, but now we are
talking about extending the language with new constructions and keywords.
Who wants language with 5000 keywords? Is it going to make our life easier?
Won't those new technologies be better off as part of the framework?
I really hope that all these talks about extending the language are said to
make us believe that the new stuff is so cool and powerful that they even
could be integrated in the language, but actually they won't do it and they'll
live them in the framework where their place is.
--
Stoitcho Goutsev (100) [C# MVP]
> Any feedback appreciated,
> Jon.
>