1

I have a code that gets the metadata of a class that includes the class methods and their parameters. And then the detected ParameterInfo is passed to another method for some other manipulations based on each parameter datatype. Following is the pseudocode snippet about which I am asking the question:

static void TypeDetector (ParameterInfo p) {

            switch (p.ParameterType)
            {
                case System.Int32:
                    do_something;
                break;

                case System.String:
                    do_something;
                break;

                default:
                    do_nothing;
                break;

            }

But I receive these compile error messages:

error CS0119: 'int' is a type, which is not valid in the given context (this error refers to case System.Int32)
error CS0119: 'string' is a type, which is not valid in the given context (and this error refers to case System.String)

I also changed "TypeDetector" method parameter from ParameterInfo to Type but I received:

'Type' does not contain a definition for 'ParameterType' and no accessible extension method 'ParameterType' accepting a first argument of type 'Type' could be found

Which the latter error message makes sense as I assume I should use ParameterInfo as the "TypeDetector" parameter. I wonder what I should change in my method?

Thanks.

Coder
  • 431
  • 4
  • 11
  • 1
    *After running the program* rather after trying to compile ... it's compile time error ... – Selvin Nov 08 '19 at 21:59
  • This looks like a case for pattern matching. Possible duplicate of https://stackoverflow.com/questions/43080505/c-sharp-7-0-switch-on-system-type –  Nov 08 '19 at 22:02
  • 1
    see pattern matching switch statement in C#7 https://learn.microsoft.com/en-us/dotnet/csharp/pattern-matching – BurnsBA Nov 08 '19 at 22:03
  • ... or if {} if else {} else... – Selvin Nov 08 '19 at 22:04

2 Answers2

1

You have a few options, none of which are really elegant.

Edit this on Sharplab

using System;
using System.Reflection;

public class C
{
    public void TypeDetector(ParameterInfo p)
    {
        if (ReferenceEquals(p.ParameterType, typeof(int)))
        {
        }
        else if (ReferenceEquals(p.ParameterType, typeof(string)))
        {
        }
        else
        {
        }
    }

    public void TypeDetector_SwitchStatement(ParameterInfo p)
    {
        // C# 7
        switch (p.ParameterType)
        {
            case Type t when ReferenceEquals(t, typeof(int)):
                break;
            case Type t when ReferenceEquals(t, typeof(string)):
                break;
            default:
                break;
        }
    }

    public string TypeDetector_SwitchExpression(ParameterInfo p)
    {
        // C# 8
        return p.ParameterType switch
        {
            Type t when ReferenceEquals(t, typeof(int)) => "int",
            Type t when ReferenceEquals(t, typeof(string)) => "string",
            _ => "Something else"
        };
    }
}
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
-1

The compiler is indicating that you cannot use the symbols int and string the way you are using them. Those symbols are not Types. They are symbols that link to types as part of compilation process. What you are looking for at compile time is the System.Type for System.String and System.Int32.

You can accomplish that using typeof(Symbol) which returns a System.Type for that symbol. That is then compared to ParameterInfo.ParameterType which is also a System.Type.

static void TypeDetector (ParameterInfo p) {
    switch (p.ParameterType)
    {
        case typeof(System.Int32):
            do_something;
            break;

        case typeof(System.String):
            do_something;
            break;

        default:
            do_nothing;
            break;

    }
}
CPerson
  • 1,222
  • 1
  • 6
  • 16