-1

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 ? enter image description here

ashutosh raina
  • 9,228
  • 12
  • 44
  • 80
  • Why don't you step through the code with a debugger? Make sure everything runs as expected (it won't and you can see the problem right then). – usr Jun 30 '12 at 19:36

1 Answers1

3

The reason is that you pass in p.PropertyType and later take source.GetType() which in this case evaluates to typeof(Type).

Probably, you want to create an overload that takes an instance of Type as the first parameter (in contrast to an object).

You can find such errors yourself by stepping through the code with a debugger. Compare actual variable values with expected ones.

usr
  • 168,620
  • 35
  • 240
  • 369
  • 3
    Thanks for the real answer :) but i am grumpy about the fact that everyone thoguht i did not try and debug it my self . Fixed it by having oveloads of Do one with Object as a parameter and other as PropertyInfo – ashutosh raina Jul 01 '12 at 05:56