C# 在C中重载方法的更好方法#
所以我有一个重载的方法。然而,这个概念相当简单。“接受其中X种数据类型中的任何一种作为第一个参数,然后接受这两种数据类型中的任何一种作为其余两个参数”。有没有更简单的方法?这很快就失控了C# 在C中重载方法的更好方法#,c#,overloading,C#,Overloading,所以我有一个重载的方法。然而,这个概念相当简单。“接受其中X种数据类型中的任何一种作为第一个参数,然后接受这两种数据类型中的任何一种作为其余两个参数”。有没有更简单的方法?这很快就失控了 //Declared MyMethod(byte[], SpecializedArgumentType, SpecializedArgumentType) and a string-> SpecializedArgumentType version of it. public stati
//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);
}
我已经尝试将任意对象作为数据类型,但是在自动完成(VisualStudioCtrl空间)中,我将无法获得良好的显式数据类型。这真的必须如此冗长和难以维护吗?也许我解决最初问题的方法需要修改?泛型呢
public static MyReturnType MyMethod<T>(T data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
{
...
}
公共静态MyReturnType MyMethod(T数据、SpecializedArgumentType第一个参数、SpecializedArgumentType第二个参数)
{
...
}
这样你就可以做到:
ushort data = 42;
var result = MyMethod<ushort>(data,firstArg,secondArg);
ushort数据=42;
var结果=MyMethod(数据,第一个参数,第二个参数);
泛型呢
public static MyReturnType MyMethod<T>(T data, SpecializedArgumentType firstArg, SpecializedArgumentType secondArg)
{
...
}
公共静态MyReturnType MyMethod(T数据、SpecializedArgumentType第一个参数、SpecializedArgumentType第二个参数)
{
...
}
这样你就可以做到:
ushort data = 42;
var result = MyMethod<ushort>(data,firstArg,secondArg);
ushort数据=42;
var结果=MyMethod(数据,第一个参数,第二个参数);
您可以创建一个具有不同类型隐式转换的数据类型,并将其用作第一个参数:
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, "", "");
您可以创建具有来自不同类型的隐式转换的数据类型,并将其用作第一个参数:
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, "", "");
需要更多的例子来说明你如何称呼它,也许你可以用泛型做些什么?需要更多的例子来说明你如何称呼它,也许你可以用泛型做些什么?这正是我想要的。谢谢!:)这正是我要找的。谢谢!:)