【问题标题】:How to fix errors in switch operator如何修复 switch 运算符中的错误
【发布时间】:2012-07-16 11:53:53
【问题描述】:

我有一个名为 config 的类,其中包含两个字符串字段,分别名为 key paramValue 和 parameterPath。

当我应用类的ChooseType方法时,该方法必须返回一个不同类型(Int或bool或String)的变量paramValue。

我实现如下:

class ConfigValue
{
    public string parameterPath;
    private string paramValue;

    public ConfigValue(string ParameterPath="empty",string ParamValue="empty")
    {
        this.parameterPath = ParameterPath;
        this.paramValue = ParameterPath;
    }

    public enum RetType { RetInt=1, RetBool, RetString };



    public  T ChooseType<T>(RetType how)
    {

        {

            switch(how)
             {
                case RetType.RetInt:

                     return int.Parse(string this.paramValue);
                        break;

                case RetType.RetBool:

                    return  Boolean.Parse(string this.paramValue);
                        break;

                case RetType.RetString:

                       return this.paramValue;
                      break;
             }

         }
    }

}

但是,我在下一行的 switch 运算符中遇到错误:

 return int.Parse(string this.paramValue);

错误:

只有赋值、调用、递增、递减和新对象表达式可以用作语句。

 return  Boolean.Parse(string this.paramValue);

错误:

表达式术语“字符串”无效。

 return this.paramValue;

错误:

无法将类型“字符串”隐式转换为“T”。

知道为什么会出现这些错误以及如何修复代码吗?

【问题讨论】:

  • "return int.Parse(string this.paramValue)" 不是一个有效的语句。应该是“return int.Parse(this.paramValue)”
  • svanryckeghem,在这种情况下,我收到此错误:无法将类型“int”隐式转换为“T”。

标签: c# .net


【解决方案1】:

知道为什么会出现这些错误吗?

编译器不知道T 是什么,也不知道如何将stringboolint 隐式转换为T

我该如何修复代码?

您可以通过显式转换为object,然后再转换为T

return (T) (object) int.Parse(string this.paramValue);

“通过”object 的要求有点奇怪 - Eric Lippert 有一个 blog post going through this in more detail

【讨论】:

    【解决方案2】:
    public T ChooseType<T>(RetType how)
    {
        switch (how)
        {
            case RetType.RetInt:
                return (dynamic)int.Parse(paramValue);
            case RetType.RetBool:
                return (dynamic)Boolean.Parse(paramValue);
            case RetType.RetString:
                return (dynamic)paramValue;
            default:
                throw new ArgumentException("RetType not supported", "how");
        }
    }
    

    在调用某些方法时不应指定参数类型。仅方法声明所需的参数类型。因此只需传递参数:

    int.Parse(this.paramValue) // you can use class members without this keyword
    int.Parse(paramValue)
    

    你还应该为你的 switch 块添加default 分支(如果不正确的参数值被传递给你的方法,你必须返回一些东西)。

    如果你已经使用了return,你也不需要break切换分支。

    对于将某些类型转换为泛型值,您应该使用dynamic,或通过对象进行双重转换:

    return (dynamic)int.Parse(paramValue);
    return (T)(object)int.Parse(paramValue);
    

    【讨论】:

      【解决方案3】:

      问题是你声明你的函数的返回类型是 T。因为 T 可以是任何类型,你不能明确地返回一个 int、string 或任何特定类型。您可能想尝试使用一个返回指令,例如

      public  T ChooseType<T>() 
      {
          return (T)this.paramValue;
      }
      

      然后,在调用函数时,指定 T,如下所示:

      int a = ChooseType<int>();
      

      string a = ChooseType<string>();
      

      请记住,如果 paramValue 不能强制转换为 T,则会引发错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-23
        • 1970-01-01
        • 2014-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多