在C#中重载方法的更好方法

时间:2013-02-25 00:15:37

标签: c# overloading

所以我有一个重载超载的方法。但是,这个概念相当简单。 “接受任何X数据类型作为第一个参数,然后为剩下的两个参数接受这两种数据类型中的任何一种”。有更简单的方法吗?这很快失控。

    //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)中获得漂亮的显式数据类型。这真的必须如此冗长且难以维护吗?也许我对初始问题的处理方法需要修改?

2 个答案:

答案 0 :(得分:4)

仿制药怎么样?

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

你可以这样做:

ushort data = 42;
var result = MyMethod<ushort>(data,firstArg,secondArg);

答案 1 :(得分: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

}

那将是一堆隐式运算符,但是你只需要两个方法的重载:

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, "", "");