所以我有一些重載的方法。但是,這個概念相當簡單。 「接受任何X數據類型作爲第一個參數,然後接受其餘兩個參數的這兩種數據類型」。有沒有更簡單的方法來做到這一點?這非常快速地失控。更好的方式來重載C#中的方法
//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鍵)的漂亮的顯式的數據類型。這真的必須如此冗長而難以維護嗎?也許我對最初的問題的方法需要修訂?
需要一點點,你會怎麼稱呼這個更例子,也許你可以用泛型做些什麼? – 2013-02-25 00:24:04