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