J
Jon Shemitz
mfc said:Suppose I have a Cookie class and a Factory Class. There are many types of
Cookies and maybe one or more Factories with a ProcessCookie Method. Suppose
all the PutInBox method does is decide what colour box to put the cookie
into. But since the colour of the box depends on whatever Factory is boxing
them (Different branding etc) the PutInBox method can't be in the Cookie
class and has to be in the Factory class. The problem is pretty soon the if
statment to decide what PutInBox method to call will be huge. What I need is
so way to dynamically cast the cookie to the right PutInBox method. Is that
possible? Something on the lines of PutInBox((typeof(cookie)) cookie)
There really isn't any way to do exactly what you describe. Probably
the closest is to use Reflection to find the PutInBox overload that
best fits the cookie type:
// neither compiled nor tested
public void ProcessCookie(Cookie cookie)
{
Type FactoryType = GetType(); // or typeof(Factory)
MethodInfo Processor = FactoryType.GetMethod(
"PutInBox",
new Type[] {cookie.GetType()} );
Processor.Invoke(this, new object[] {cookie});
}