I have used this form of switch-case on rare occasion. Even then I have found another way to do what I wanted. If you find that this is the only way to accomplish what you need, I would recommend @Mark H's solution.
If this is intended to be a sort of factory creation decision process, there are better ways to do it. Otherwise, I really can't see why you want to use the switch on a type.
Here is a little example expanding on Mark's solution. I think it is a great way to work with types:
Dictionary<Type, Action> typeTests;
public ClassCtor()
{
typeTests = new Dictionary<Type, Action> ();
typeTests[typeof(int)] = () => DoIntegerStuff();
typeTests[typeof(string)] = () => DoStringStuff();
typeTests[typeof(bool)] = () => DoBooleanStuff();
}
private void DoBooleanStuff()
{
//do stuff
}
private void DoStringStuff()
{
//do stuff
}
private void DoIntegerStuff()
{
//do stuff
}
public Action CheckTypeAction(Type TypeToTest)
{
if (typeTests.Keys.Contains(TypeToTest))
return typeTests[TypeToTest];
return null; // or some other Action delegate
}
answered 2010-12-18T15:03:09.760