Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use switch-case on a Type? [duplicate]

Tags:

c#

Possible Duplicate:
Is there a better alternative than this to 'switch on type'?

I need to iterate through all properties of my class and to check if its type of int the i need to do something, if its string .. then do something. I need it using switch-case. Here i am using switch in the following manner, but it asks for some constant. see the code below:

 public static bool ValidateProperties(object o)
{
    if(o !=null)
    {
        var sourceType = o.GetType();
        var properties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Static);
        foreach (var property in properties)
        {
            var type = property.GetType();
            switch (type)
            {
                *case typeof(int):* getting error here
                    // d
            }
        }
    }
}

Also i want to know , what check should I use, typeof(int) or typeof(Int32)?


2 Answers

You cannot use a switch block to test values of type Type. Compiling your code should give you an error saying something like:

A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type

You'll need to use if-else statements instead.

Also: typeof(int) and typeof(Int32) are equivalent. int is a keyword and Int32 is the type name.

UPDATE

If you expect that most types will be intrinsic you may improve performance by using a switch block with Type.GetTypeCode(...).

For example:

switch (Type.GetTypeCode(type))
{
    case TypeCode.Int32:
        // It's an int
        break;

    case TypeCode.String:
        // It's a string
        break;

    // Other type code cases here...

    default:
        // Fallback to using if-else statements...
        if (type == typeof(MyCoolType))
        {
            // ...
        }
        else if (type == typeof(MyOtherType))
        {
            // ...
        } // etc...
}
like image 186
Mårten Wikström Avatar answered Sep 14 '25 03:09

Mårten Wikström


A good and extensible way to do this is to make a dictionary of types and delegates of appropriate type, based on what you want to do with values of that type.

For example:

var typeProcessorMap = new Dictionary<Type, Delegate>
{
    { typeof(int), new Action<int>(i => { /* do something with i */ }) },
    { typeof(string), new Action<string>(s => { /* do something with s */ }) },
};

And then:

void ValidateProperties(object o)
{
    var t = o.GetType();
    typeProcessorMap[t].DynamicInvoke(o); // invoke appropriate delegate
}

This solution is extensible, configurable even at run time, and as long as you keep the keys and types of delegate values in typeProcessorMap correctly matched is also type safe.

See it in action.

like image 38
Jon Avatar answered Sep 14 '25 05:09

Jon