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>
[quoted text, click to view] > 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