【问题标题】:c# Extension methodc# 扩展方法
【发布时间】:2010-08-27 07:41:26
【问题描述】:

我正在尝试测试我的扩展方法,该方法将字符串列表转换为逗号分隔的字符串:

public static class Extensions
{
      public static string ToCommaString<T>(this IList<T> input)
      {
        StringBuilder sb = new StringBuilder();
        foreach (T value in input)
        {
            sb.Append(value);
            sb.Append(",");
        }
        return sb.ToString();
      }
      public void TestExtension()
      {
        IList test=new List<string>();
        //test.ToCommaString doesnt appear
      }
}

问题是在 TestExtension 方法中我不能使用 ToCommaString 方法。

你知道发生了什么吗?

我可以为我的所有 Web 应用程序提供在 web.config 中注册的扩展方法或类似的东西吗?

提前致谢。

最好的问候。

何塞

【问题讨论】:

    标签: asp.net asp.net-mvc-2 oop extension-methods


    【解决方案1】:

    您声明您的列表类型错误(非泛型):

    IList test=new List<string>(); 
    

    应该是

    IList<String> test=new List<string>(); 
    

    【讨论】:

      猜你喜欢
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 2014-05-13
      • 2016-10-30
      • 2020-04-15
      • 2010-12-03
      相关资源
      最近更新 更多