Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 泛型函数重载_C#_Generics_Overloading - Fatal编程技术网

C# 泛型函数重载

C# 泛型函数重载,c#,generics,overloading,C#,Generics,Overloading,我有以下问题: 有几个颜色界面具有基本界面IColor public interface IColor { } public interface IColor3 : IColor { } public interface IColor4 : IColor { } 某些算法仅支持对某些颜色类型进行处理: public static Image<TColor, byte> Bla<TColor>(this Image<TColor, byte> img,

我有以下问题:

有几个颜色界面具有基本界面IColor

public interface IColor { }
public interface IColor3 : IColor { }
public interface IColor4 : IColor { }
某些算法仅支持对某些颜色类型进行处理:

    public static Image<TColor, byte> Bla<TColor>(this Image<TColor, byte> img, bool inPlace = true)
        where TColor : IColor4
    {
       //do something
    }

    public static Image<TColor, byte> Bla<TColor>(this Image<TColor, byte> img, bool inPlace = true)
        where TColor : IColor3
    {
       //do something
    }
公共静态图像Bla(此图像img,bool inPlace=true)
其中t颜色:IColor4
{
//做点什么
}
公共静态图像Bla(此图像img,bool inPlace=true)
其中TColor:IColor3
{
//做点什么
}
当我试图编译时,我得到一个错误,即已经定义了一个具有相同参数的函数。
如何解决此问题?

方法重载仅基于参数和名称。这意味着您要编写一个复制的方法,而不是重载

我建议您为此编辑代码:

public static Image<IColor4, byte> Bla(this Image<IColor4, byte> img, bool inPlace = true)
{
   //do something
}

public static Image<IColor3, byte> Bla(this Image<IColor3, byte> img, bool inPlace = true)
{
   //do something
}
公共静态图像Bla(此图像img,bool inPlace=true)
{
//做点什么
}
公共静态图像Bla(此图像img,bool inPlace=true)
{
//做点什么
}
或:

公共静态图像Bla(此图像img,bool inPlace=true)
其中TColor:IColor
{
if(TColor==typeof(SomeSpecificType))
{
//在这里做一些具体的事情。
}
}

如注释中所述,您提供的示例实际上并不构成方法重载,因为方法重载基于方法签名,该签名仅包含参数

您可以只使用一种方法,并对从中的IColor派生的接口进行测试

public static Image<TColor, byte> Bla<TColor>(this Image<TColor, byte> img, bool inPlace = true)
    where TColor : IColor
{
    if (img is IColor3)
    {
        //do something
    }
    if (img is IColor4)
    {
        //do something
    }
}
公共静态图像Bla(此图像img,bool inPlace=true)
其中TColor:IColor
{
如果(img为IColor3)
{
//做点什么
}
if(img为IColor4)
{
//做点什么
}
}

为了实现这一点,您需要两个不同(但可能相似)的泛型类

public static Image4<TColor, byte> Bla<TColor>(this Image<TColor, byte> img, bool inPlace = true)
    where TColor : IColor4
{
   //do something
}

public static Image3<TColor, byte> Bla<TColor>(this Image<TColor, byte> img, bool inPlace = true)
    where TColor : IColor3
{
   //do something
}
公共静态图像4 Bla(此图像img,bool inPlace=true)
其中t颜色:IColor4
{
//做点什么
}
公共静态图像3 Bla(此图像img,bool inPlace=true)
其中TColor:IColor3
{
//做点什么
}

也取决于你想要什么。这两个类可以从一个公共基继承,该基可以执行任何可以在type
IColor

上执行的公共功能。请参阅以下博文:@MarcinJuraszek,John Rasch:非常感谢
public static Image4<TColor, byte> Bla<TColor>(this Image<TColor, byte> img, bool inPlace = true)
    where TColor : IColor4
{
   //do something
}

public static Image3<TColor, byte> Bla<TColor>(this Image<TColor, byte> img, bool inPlace = true)
    where TColor : IColor3
{
   //do something
}