0

だから私はいくつかの重いオーバーロードを伴うメソッドを持っています。ただし、概念はかなり単純です。「最初の引数としてXデータ型のいずれかを受け入れ、次に残りの2つの引数に対してこれら2つのデータ型のいずれかを受け入れます」。これを行うためのより簡単な方法はありますか?これは非常に速く手に負えなくなっています。

    //Declared MyMethod(byte[], SpecializedArgumentType, SpecializedArgumentType) and a string-> SpecializedArgumentType  version of it.
    public static MyReturnType MyMethod(bool data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(bool data, String firstArg, String secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(short data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(short data, String firstArg, String secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(ushort data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(ushort data, String firstArg, String secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(int data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(int data, String firstArg, String secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(uint data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(uint data, String firstArg, String secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(long data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(long data, String firstArg, String secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(ulong data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(ulong data, String firstArg, String secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(float data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(float data, String firstArg, String secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(double data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(double data, String firstArg, String secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(char data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }
    public static MyReturnType MyMethod(char data, String firstArg, String secondArg)
    {
        return MyMethod(BitConverter.GetBytes(data), firstArg, secondArg);
    }

データ型として任意のオブジェクトを取り込もうとしましたが、オートコンプリート(Visual studioのctrl-space)で明示的なデータ型を取得できません。これは本当に冗長で維持するのが難しい必要がありますか?たぶん、最初の問題への私のアプローチは修正が必要ですか?

4

2 に答える 2

4

ジェネリックはどうですか?

public static MyReturnType MyMethod<T>(T data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
{
    ...
}

そうすれば、次のことができます:

ushort data = 42;
var result = MyMethod<ushort>(data,firstArg,secondArg);
于 2013-02-25T00:28:49.680 に答える
0

さまざまな型からの暗黙的な変換を持つデータ型を作成し、それを最初のパラメーターとして使用できます。

public class MyFirstParameter {

  public byte[] Bytes { get; private set; }

  private MyFirstParameter (byte[] bytes){
    Bytes = bytes;
  }

  public static implicit operator MyFirstParameter(int value) {
    return new MyFirstParameter(BitConverter.GetBytes(value));
  }

  public static implicit operator MyFirstParameter(long value) {
    return new MyFirstParameter(BitConverter.GetBytes(value));
  }

  // and a few more types

}

これは一連の暗黙的な演算子になりますが、必要なメソッドのオーバーロードは 2 つだけです。

public static MyReturnType MyMethod(MyFirstParameter data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg) {
  return MyMethod(data.Bytes, firstArg, secondArg);
}

public static MyReturnType MyMethod(MyFirstParameter data, String firstArg, String secondArg) {
  return MyMethod(data.Bytes, firstArg, secondArg);
}

あたかもその型のパラメーターがあるかのように、暗黙的な変換がある型のいずれかでメソッドを呼び出すことができます。

MyMethod(42, "", "");
于 2013-02-25T01:01:03.303 に答える