有些内置方法对我不起作用,我在我的应用程序中使用旧版本的.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)必须在非通用静态类
中定义扩展方法
如何解决这些问题?请帮帮我。
谢谢。
答案 0 :(得分:0)
非泛型静态类意味着您正在创建一个不使用模板的类。
e.g。 List是一个泛型类,但MemoryStream或很多类都不是通用的。
此线程中已经给出了嵌套类的答案。
答案 1 :(得分:-1)
尝试在任何其他类中保留“static void Main()”。我的意思是你试图用来创建扩展的那个。
答案 2 :(得分:-1)
看起来你的主要方法也在Retrieve类中。我建议为扩展方法创建一个单独的类,如下所示:
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);
}
}
}