Type based dispatch

S

Scott Graham

Hi,

In the following code, is there any way to get rid of the method marked
below (that has signature static void Func(Base o))? It seems that the
language should be able to dispatch to the correct function for me
based on the actual type of the object, but I can't figure out how to
write that.

(I'm aware that making Func a virtual member of Base, A, and B would
work, but there are a great many different "Func"s that have a lot more
to do with each other than they do with the types A/B so it doesn't
make very much sense to do it that way.)

Thanks for any suggestions or pointers,
scott

---------------------
using System;
using System.Collections.Generic;

class Base { }
class A : Base { }
class B : Base { }

class Program
{
static void Func(A a)
{
System.Console.WriteLine("in Func for A");
}

static void Func(B b)
{
System.Console.WriteLine("in Func for B");
}

// *** This is the function I want to get rid of ***
static void Func(Base o)
{
if (o is A) { Func((A)o); return; }
if (o is B) { Func((B)o); return; }
throw new Exception("bad type");
}

static void Main(string[] args)
{
List<Base> l = new List<Base>();
l.Add(new A());
l.Add(new B());
foreach (Base o in l)
{
Func(o);
}
}
}
---------------------
 
R

Randy A. Ynchausti

Scott,
In the following code, is there any way to get rid of the method marked
below (that has signature static void Func(Base o))?

Well, there are several ways to do this. I have implemented only one of
them in the following code. I happened to use reflection in the code
snippet. And I do readily admit that this approach is not for the faint of
heart.

I hope that helps.

Regards,

Randy

====================================

using System;
using System.Collections.Generic;
using System.Text;
namespace InheritanceQuestion
{
class Base
{
public static void Func(Base b)
{
System.Console.WriteLine("in Func for Base");
}
}
class A : Base
{
public static void Func(A a)
{
System.Console.WriteLine("in Func for A");
}
}
class B : Base
{
public static void Func(B b)
{
System.Console.WriteLine("in Func for B");
}
}
class Program
{
static void Main(string[] args)
{
List<Base> l = new List<Base>();
l.Add(new A());
l.Add(new B());
foreach (Base o in l)
{
o.GetType().GetMethod("Func", System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.Public).Invoke(o, new object[] {o});
}
}
}
}
 
N

Nick Hounsome

Scott Graham said:
Hi,

In the following code, is there any way to get rid of the method marked
below (that has signature static void Func(Base o))? It seems that the
language should be able to dispatch to the correct function for me
based on the actual type of the object, but I can't figure out how to
write that.

(I'm aware that making Func a virtual member of Base, A, and B would
work, but there are a great many different "Func"s that have a lot more
to do with each other than they do with the types A/B so it doesn't
make very much sense to do it that way.)

Yes it does - just use polymorphism but make all the Func common stuff
private in the base class. Your polymorphic methods can then just call the
appropriate private base methods.
 
S

Scott Graham

Hi

Thanks for your reply, that's more or less what I was looking for. It
seems a bit heavy, having to load up all the reflection stuff, but what
can you do I guess.

I'd prefer to have the "Func"s in Program rather than inside a
particular data class (A/B), but presumably I can do that with the same
mechanism and do a bit of searching on the parameter reflection info
and then do something similar to what you wrote.

Thanks for your help,
scott.
Scott,
In the following code, is there any way to get rid of the method marked
below (that has signature static void Func(Base o))?

Well, there are several ways to do this. I have implemented only one of
them in the following code. I happened to use reflection in the code
snippet. And I do readily admit that this approach is not for the faint of
heart.

I hope that helps.

Regards,

Randy

====================================

using System;
using System.Collections.Generic;
using System.Text;
namespace InheritanceQuestion
{
class Base
{
public static void Func(Base b)
{
System.Console.WriteLine("in Func for Base");
}
}
class A : Base
{
public static void Func(A a)
{
System.Console.WriteLine("in Func for A");
}
}
class B : Base
{
public static void Func(B b)
{
System.Console.WriteLine("in Func for B");
}
}
class Program
{
static void Main(string[] args)
{
List<Base> l = new List<Base>();
l.Add(new A());
l.Add(new B());
foreach (Base o in l)
{
o.GetType().GetMethod("Func", System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.Public).Invoke(o, new object[] {o});
}
}
}
}
 
S

Scott Graham

For anyone interested, this is the (probably horrendously slow) code I
ended up with.

----------
using System;
using System.Collections.Generic;
using System.Reflection;

class Base { }
class A : Base { }
class B : Base { }

class Program
{
private static void Func(A a)
{
System.Console.WriteLine("in Func for A");
}

private static void Func(B b)
{
System.Console.WriteLine("in Func for B");
}

private static bool DelegateToSearchCriteria(
System.Reflection.MemberInfo objMemberInfo,
object objSearch)
{
if (objMemberInfo.Name.ToString() == objSearch.ToString())
return true;
else
return false;
}

private static Dictionary
<
string,
Dictionary said:
msDispatch
= new Dictionary<string, Dictionary<Type, MethodInfo>>();

private static void MakeDispatchFor(string name)
{
MemberInfo[] arrayMemberInfo = typeof(Program).FindMembers(
System.Reflection.MemberTypes.Method,
System.Reflection.BindingFlags.Static
| System.Reflection.BindingFlags.NonPublic,
new MemberFilter(DelegateToSearchCriteria), name);
foreach (MethodInfo mi in arrayMemberInfo)
{
ParameterInfo[] pi = mi.GetParameters();
if (pi.Length == 1)
{
if (!msDispatch.ContainsKey(name))
{
msDispatch[name] = new Dictionary<Type, MethodInfo>();
}
msDispatch[name][pi[0].ParameterType] = mi;
}
}
}

private static object Call(string name, object o)
{
return msDispatch[name][o.GetType()]
.Invoke(null, new object[] { o });
}

static void Main(string[] args)
{
MakeDispatchFor("Func");

List<Base> l = new List<Base>();
l.Add(new A());
l.Add(new B());
foreach (Base o in l)
{
Call("Func", o);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top