2013-02-25 51 views
0

所以我有一些重載的方法。但是,這個概念相當簡單。 「接受任何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鍵)的漂亮的顯式的數據類型。這真的必須如此冗長而難以維護嗎?也許我對最初的問題的方法需要修訂?

+2

需要一點點,你會怎麼稱呼這個更例子,也許你可以用泛型做些什麼? – 2013-02-25 00:24:04

回答

4

那麼仿製藥呢?

​​

這樣,你可以這樣做:

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

這正是我正在尋找的。謝謝! :) – Automatico 2013-02-25 01:24:05

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, "", ""); 
相關問題