0

Possible Duplicate:
Using Case/Switch and GetType to determine the object

If I hope to rewrite my codes with syntax if (...) else if (...) into the syntax with switch (...) case for following codes, how to achieve it ?

void Foo(object aObj) {
      if (aObj is Dictionary<int,object> ) {

      } else if (aObj is string) {

      } else if (aObj is int) {

      } else if (aObj is MyCustomClass) {

      } else {
         throw new Exception("unsupported class type.");
      }
   }
Community
  • 1
  • 1
AechoLiu
  • 17,522
  • 9
  • 100
  • 118

5 Answers5

5

The only way I found until today is by doing something like this:

In your class define a Dictionary:

 private static Dictionary<Type, int> typeMap = new Dictionary<Type, int>();

then in your class Constructor fill your Dictionary with whatever Types you use:

typeMap.Add(typeof(float), 0);
typeMap.Add(typeof(OpenTK.Graphics.Color4), 1);
typeMap.Add(typeof(LightStructure), 2);
typeMap.Add(typeof(Camera), 3);

in your code you then can use:

object values = new OpenTK.Graphics.Color4(1.0,1.0,1.0)
switch (typeMap[values.GetType()])
{
  ...
}

in this example you would end up in case 1.

lunactic
  • 316
  • 1
  • 5
0

In C#, you can't switch or implement cases based on System.Type.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
0

You can use the name of the type:

void Foo(object aObj) {
  switch (aObj.GetType().Name) {
    case "Dictionary`2":
      if (String.Join(",",aObj.GetType().GetGenericArguments().Select(a => a.Name)) == "Int32,String") {
        // ok
      } else {
        throw new ArgumentException("unsupported types in dictionary.");
      }
      break;
    case "String" {
      break;
    case "Int32" {
      break;
    case "MyCustomClass" {
      break;
    default:
      throw new ArgumentException("unsupported class type.");
  }

}

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

you can do something like this

object s1 = "TestString";
   var k = s1.GetType().ToString();
    switch(k)
    {
    case  "System.String":

        {
        Console.WriteLine("string");
             break;
        }
     //Add Other case also
    default:
        {
            Console.WriteLine("Not Sting");
            break;
        }
    }
anishMarokey
  • 11,279
  • 2
  • 34
  • 47
0

Here a sample where you don't need a switch at all:

        var typeBasedFunctionDictionary = new Dictionary<Type, Action>();
        typeBasedFunctionDictionary.Add(typeof(string), () => Console.WriteLine("string..."));
        typeBasedFunctionDictionary.Add(typeof(decimal), () => Console.WriteLine("decimal..."));
        typeBasedFunctionDictionary.Add(typeof(int), () => Console.WriteLine("int..."));

        //if (!typeBasedFunctionDictionary.ContainsKey(typeof(object)))
        //{
        //    throw (new InvalidOperationException("Type not supported..."));
        //}
        typeBasedFunctionDictionary["foo".GetType()]();
        typeBasedFunctionDictionary[10.2m.GetType()]();
        typeBasedFunctionDictionary[10.GetType()]();