2013-02-25 100 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, "", "");