471,321 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,321 software developers and data experts.

Type based dispatch

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);
}
}
}
---------------------

Feb 1 '06 #1
4 1708
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});
}
}
}
}
Feb 1 '06 #2

"Scott Graham" <sg*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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.

Feb 1 '06 #3
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.

Randy A. Ynchausti wrote:
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});
}
}
}
}


Feb 2 '06 #4
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<Type, MethodInfo>
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);
}
}
}

----------

scott

Feb 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Joakim Hove | last post: by
reply views Thread by Tim Roberts | last post: by
3 posts views Thread by Gernot Frisch | last post: by
2 posts views Thread by Dave | last post: by
14 posts views Thread by Joseph Turian | last post: by
11 posts views Thread by Frederic Rentsch | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.