【问题标题】:'string' does not contain a definition for/ Error in C#“字符串”不包含 C# 中的/错误的定义
【发布时间】:2014-07-21 21:00:57
【问题描述】:

我正在尝试在 C# 中创建一个查询字符串。我在 StackOverflow 中找到了这段代码,我真的很喜欢它并想在我的项目中使用它。但是我得到一个错误,我坚持下去。这是代码

public static string AddQueryParam(this string source, string key, string value)
{
    string delim;
    if ((source == null) || !source.Contains("?"))
    {
        delim = "?";
    }
    else if (source.EndsWith("?") || source.EndsWith("&"))
    {
        delim = string.Empty;
    }
    else
    {
        delim = "&";
    }

    return source + delim + HttpUtility.UrlEncode(key)
        + "=" + HttpUtility.UrlEncode(value);
}

private string QueryStringCreator()
{
    string queryString = "http://www.something.com/something.html"
    .AddQueryParam("name", "jason")//I get the error here
        .AddQueryParam("age","26");

    return queryString;
}

错误是:

'string' 不包含 'AddQueryParam' 的定义,并且没有 扩展方法“AddQueryParam”接受类型的第一个参数 可以找到“字符串”(您是否缺少 using 指令或 汇编参考?)

我该如何解决这个问题?谢谢。

【问题讨论】:

  • 你是using扩展方法类所在的命名空间吗?您是否遇到任何其他编译器错误?

标签: c#


【解决方案1】:

要制作扩展方法AddQueryParam,请将其放入单独的静态类中。

static class StringExtension
{
    public static string AddQueryParam(this string source, string key, string value)
    {
        // ...
    }
}

顺便说一句,我希望发布的代码应该给出另一个错误:

扩展方法必须定义在非泛型静态类中

【讨论】:

    【解决方案2】:

    Extension Method 需要在非通用静态类中声明。

    来自C# Specification

    26.2.1 声明扩展方法

    扩展方法是通过指定关键字 this 作为第一个参数的修饰符来声明的 方法。扩展方法只能在非泛型中声明, 非嵌套静态类。下面是一个静态的例子 声明两个扩展方法的类。

    这样声明:

    public static class StringExtensions
    {
       public static string AddQueryParam(this string source, string key, string value)
       {
          string delim;
          if ((source == null) || !source.Contains("?"))
          {
              delim = "?";
          }
          else if (source.EndsWith("?") || source.EndsWith("&"))
          {
              delim = string.Empty;
          }
          else
          {
             delim = "&";
          }
    
          return source + delim + HttpUtility.UrlEncode(key)
            + "=" + HttpUtility.UrlEncode(value);
       }
    }
    

    【讨论】:

      【解决方案3】:

      AddQueryParam 是一个扩展 方法。所以你应该把它放在static 类中。

      static public class Extensions
      {
          public static string AddQueryParam(this string source, string key, string value)
          {
              string delim;
              if ((source == null) || !source.Contains("?"))
              {
                  delim = "?";
              }
              else if (source.EndsWith("?") || source.EndsWith("&"))
              {
                  delim = string.Empty;
              }
              else
              {
                  delim = "&";
              }
          } 
          return source + delim + HttpUtility.UrlEncode(key)
          + "=" + HttpUtility.UrlEncode(value);
      }
      

      有关扩展方法的更多信息,请查看here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-03-06
        • 2012-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-14
        • 2019-10-06
        相关资源
        最近更新 更多