我尝试从 Main() 调用静态类下定义的 Extension 方法,它起作用了。现在我想在我的应用程序中使用它,为此我需要将扩展方法设为静态方法(因为我的应用程序中没有定义静态类)并从 Main() 调用它。
这是我正在尝试的:
public class Get
{
public static void CopyTo(Stream input, Stream output) //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 ()
{
////I' m just mentioning a small part of my code
////Please ignore about the variables(url, baseURL,authenticatestr...) those are not declared here, they have been declared at some other part in the code
/////Here in the main method I have a call to the above method
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (url);
request = (HttpWebRequest)WebRequest.Create (baseURL + uri);
request.Headers.Add ("Authn", authenticateStr);
request.Accept = "";
request.Method = "GET";
webResponse = (HttpWebResponse)request.GetResponse();
using (MemoryStream ms = new MemoryStream())
using (FileStream outfile = new FileStream("1" , FileMode.Create)) {
webResponse.GetResponseStream().CopyTo(ms);///Here I need to call my method
outfile.Write(ms.GetBuffer(), 0, (int)ms.Length);
}
但这仍在尝试调用 .NetFramework CopyTo() 方法。如何调用代码中定义的方法?请帮我。
谢谢你。