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) 拡張メソッドは最上位の静的クラスで定義する必要があります。「取得」はネストされたクラスです。

「取得」クラスが入れ子になった理由がわかりません。

2) 拡張メソッドは、非ジェネリックな静的クラスで定義する必要があります

これらの問題を解決するにはどうすればよいですか?私を助けてください。

ありがとうございました。

4

3 に答える 3

-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 に答える