0

一些内置方法对我不起作用,我正在为我的应用程序使用旧版本的 .NetFramework,它没有一些新方法。因此,我正在尝试创建覆盖内置方法的扩展方法。但我面临一些问题。这是代码:

using System; 
using System.IO; 
using System.Net;
using System.Xml;
using System.Text;  
using System.Collections.Generic;
namespace API{
public static class Retrive 
{               
    // some variables

    public static void CopyTo(this Stream input, Stream output)///Extension method
    {

        byte[] buffer = new byte[32768];  
        int read;

        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write (buffer, 0, read);
        }
    }   

    public static void Main ()
    {
          string url = "https://";          
          string baseURL = "";   
          string authenticateStr = "";

        try 
        {
            ///
            }


        catch (WebException e) 
        {
            using (WebResponse response = e.Response) 
            {
                ////
            }
        }
    }  // end main()
}  // end class

}//结束命名空间

我遇到的错误是

1) 扩展方法必须定义在顶级静态类中;'Retrive' 是一个嵌套类。

我不明白为什么“检索”类变得嵌套。

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

如何解决这些问题?请帮我。

谢谢你。

4

3 回答 3

0

非通用静态类意味着您正在创建一个不使用模板的类。

例如 List 是一个通用类,但 MemoryStream 或很多类不是通用的。

嵌套类答案已在此线程中给出。

于 2013-04-24T07:11:59.573 回答
-1

尝试在任何其他类中保留“static void Main()”。我的意思是除了你试图用来创建扩展的那个。

于 2013-04-24T07:04:27.860 回答
-1

看起来您的主要方法也在 Retriev 类中。我建议为扩展方法创建一个单独的类,如下所示:

public static class ExtMethods 
{
  public static void CopyTo(this Stream input, Stream output)///Extension method
  {
    byte[] buffer = new byte[32768];  
    int read;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
       output.Write (buffer, 0, read);
    }
  }  
}
于 2013-04-24T07:05:16.397 回答