Your program doesn't compile. Fail.
Here's a version that does:
public class MyClass
{
private delegate object Handler(object obj);
private readonly Dictionary<Type, Handler> handlerBunch =
new Dictionary<Type, Handler>();
public MyClass()
{
handlerBunch.Add(typeof(int), DoSomethingWithInt);
handlerBunch.Add(typeof(float), DoSomethingWithFloat);
handlerBunch.Add(typeof(DateTime), DoSomethingWithDateTime);
handlerBunch.Add(typeof(FileStream), DoSomethingWithFileStream);
}
public object DoSomething(object obj)
{
if (!handlerBunch.ContainsKey(obj.GetType()))
{
throw new ArgumentException("obj");
}
else
{
return handlerBunch[obj.GetType()](obj);
}
}
public object DoSomethingWithInt(object arg)
{
return (int)arg + 7;
}
public object DoSomethingWithFloat(object arg)
{
return (float)arg;
}
public object DoSomethingWithDateTime(object arg)
{
return arg.ToString();
}
public object DoSomethingWithFileStream(object arg)
{
(arg as FileStream).Close();
return null;
}
}
There's probably a clever way to use a delegate instead of Dictionary.ContainsKey(Type) to enable F#/Haskell-style pattern matching.