No delegate "base class" ?!?!?!

  • Thread starter Thread starter Stephen Johns
  • Start date Start date
S

Stephen Johns

I want to have a Hashtable whose keys are strings and whose values are
delegates.

Well, ok, I have that:

dels = new Hastable();
dels.Add( "key", new Foo1Delegate(MyFoo1) );
dels.Add( "key", new Foo2Delegate(MyFoo2) );
dels.Add( "key", new Foo3Delegate(MyFoo3) );

This works.

BUT! I can't get the darned things back out!

I want to do:

delegate d = (delegate)dels["key"];
d( arg );

The first line results in a syntax error.


MyFoo1 f = (MyFoo1)dels["key"];
f( arg );

works, but I have to know the delegate type to get it out - negating
what I am trying to accomplish.

All the methods I will call have the same arguments.

Basically looking for C/Function-Pointer kind of thing.

Anyone know how to do this in C#?

Any help greatly appreciated.
 
Stephen Johns said:
I want to have a Hashtable whose keys are strings and whose values are
delegates.

Well, ok, I have that:

dels = new Hastable();
dels.Add( "key", new Foo1Delegate(MyFoo1) );
dels.Add( "key", new Foo2Delegate(MyFoo2) );
dels.Add( "key", new Foo3Delegate(MyFoo3) );

This works.

BUT! I can't get the darned things back out!

I want to do:

delegate d = (delegate)dels["key"];
d( arg );

Use the type Delegate. You will probably have to use DynamicInvoke to
perform the work, but
it should work(untested).
The first line results in a syntax error.


MyFoo1 f = (MyFoo1)dels["key"];
f( arg );

works, but I have to know the delegate type to get it out - negating
what I am trying to accomplish.

All the methods I will call have the same arguments.

Basically looking for C/Function-Pointer kind of thing.

Anyone know how to do this in C#?

Any help greatly appreciated.
 
Stephen Johns said:
I want to have a Hashtable whose keys are strings and whose values are
delegates.

Well, ok, I have that:

dels = new Hastable();
dels.Add( "key", new Foo1Delegate(MyFoo1) );
dels.Add( "key", new Foo2Delegate(MyFoo2) );
dels.Add( "key", new Foo3Delegate(MyFoo3) );

This works.

BUT! I can't get the darned things back out!

I want to do:

delegate d = (delegate)dels["key"];
d( arg );

The first line results in a syntax error.


MyFoo1 f = (MyFoo1)dels["key"];
f( arg );

works, but I have to know the delegate type to get it out - negating
what I am trying to accomplish.

Thus the "typesafeness" of delegates.

Why don't you just define an interface, and store interface references
insead of delegates.

David
 
Stephen Johns said:
I want to have a Hashtable whose keys are strings and whose values are
delegates.

Well, ok, I have that:

dels = new Hastable();
dels.Add( "key", new Foo1Delegate(MyFoo1) );
dels.Add( "key", new Foo2Delegate(MyFoo2) );
dels.Add( "key", new Foo3Delegate(MyFoo3) );

This works.

BUT! I can't get the darned things back out!

<snip>

I think you're looking for the Delegate class. Try this, for instance:

using System;
using System.Collections;

class Test
{
delegate void X();
delegate void Y();

static void Main()
{
ArrayList al = new ArrayList();
al.Add(new X(Foo));
al.Add(new Y(Bar));
foreach (Delegate d in al)
{
d.DynamicInvoke(null);
}
}

static void Foo()
{
Console.WriteLine ("Hello");
}

static void Bar()
{
Console.WriteLine ("There");
}
}
 
Stephen,

As Jon and Daniel note, you can use Delegate and then call DynamicInvoke()
on the delegate. You should note that this is considerably slower than
calling directly through a delegate (at least 10x slower, and perhaps even
100x slower) because of all the work required to do a late-bound call.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks for the info guys! That is what I am looking for.

I guess I am just old school, "typesafeness" hmmmm... sounds like
training wheels... Give me a (void *) anyday, I'll remember what I put
in there I'd never accidentally try and "call" a piece of data! hahahaha
 
Stephen,
Have you tried something like (tested in VS.NET 2003):

delegate void Foo1Delegate(int i);
delegate void Foo2Delegate(char ch);
delegate void Foo3Delegate(string s);

static void Main()
{
Hashtable dels = new Hashtable();
dels.Add( "key1", new Foo1Delegate(MyFoo1) );
dels.Add( "key2", new Foo2Delegate(MyFoo2) );
dels.Add( "key3", new Foo3Delegate(MyFoo3) );

object o = dels["key2"];
if (o is Foo1Delegate)
{
Foo1Delegate d = (Foo1Delegate)o;
d(1);
}
else if (o is Foo2Delegate)
{
Foo2Delegate d = (Foo2Delegate)o;
d(' ');
}
else if (o is Foo3Delegate)
{
Foo3Delegate d = (Foo3Delegate)o;
d("");
}
}

static void MyFoo1(int i)
{
}

static void MyFoo2(char ch)
{
}

static void MyFoo3(string s)
{
}

Hope this helps
Jay
 
Back
Top