Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8191105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:50:45+00:00 2026-06-07T03:50:45+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:50:46+00:00Added an answer on June 7, 2026 at 3:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I took the example code from the Kendo UI demos at http://demos.kendoui.com/web/grid/remote-data.html , binding
I'm using backbone.js, underscore.js and jQuery. I took the following sample code from http://jsfiddle.net/thomas/C9wew/6/
I took the following code from the examples page on Asio class tcp_connection :
Following code is to save image took from camera into photo album. if ([mediaType
While the following code is saving the image I took from my application into
Running my script through Devel::NYTProf showed that the following portion of code took up
Following on from some comments of late, I have took the plunge and started
I took some code from my C# Windows form app which uses the Application
The following question is from a quiz I took a few weeks ago and
I was looking at the sample code for a lock-free queue at: http://drdobbs.com/high-performance-computing/210604448?pgno=2 (Also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.