I took the following code from http://blogs.msdn.com/b/jaredpar/archive/2008/05/16/switching-on-types.aspx
static class TypeSwitch {
public class CaseInfo {
public bool IsDefault { get; set; }
public Type Target { get; set; }
public Action<object> Action { get; set; }
}
public static void Do(object source, params CaseInfo[] cases) {
var type = source.GetType();
foreach (var entry in cases) {
if (entry.IsDefault || type == entry.Target) {
entry.Action(source);
break;
}
}
}
public static CaseInfo Case<T>(Action action) {
return new CaseInfo() {
Action = x => action(),
Target = typeof(T)
};
}
public static CaseInfo Case<T>(Action<T> action) {
return new CaseInfo() {
Action = (x) => action((T)x),
Target = typeof(T)
};
}
public static CaseInfo Default(Action action) {
return new CaseInfo() {
Action = x => action(),
IsDefault = true
};
}
}
and i am doing the following
TypeSwitch.Do(
p.PropertyType,
TypeSwitch.Case<Int32>(() => DoSomething),
TypeSwitch.Case<Double>(() => DoSomething),
TypeSwitch.Case<float>(() => DoSomething),
TypeSwitch.Case<bool>(() => DoSomething),
TypeSwitch.Case<String>(() => DoSomething),
TypeSwitch.Default(() => {
TypeSwitch.Do
(
p.PropertyType,
TypeSwitch.Case<Object[]>(() => Do()),
TypeSwitch.Case<IEnumerable<Object>>(() => Do()),
TypeSwitch.Case<IDictionary<Object, Object>>(() => Do()
)
);
p is of type PropertyInfo
. Even when p.PropertyType is String it goes to the Default Case .. Why ? Shouldn't it break ? If there is something missing in the TypeSwitch , how can i fix that ?
Edit
I have no idea why everyone assumed that i did not debug it first and went on a down voting spree. But i did put in significant amount of time on this . I checked , i am only passing ProprtyType as String it loops through CaseInfo for everything then it goes to Do and executes the Default Case .. The question is what am i missing ?