C# 重写C中类型的静态泛型方法

C# 重写C中类型的静态泛型方法,c#,generics,struct,static-methods,overriding,C#,Generics,Struct,Static Methods,Overriding,我正试图为颜色转换Bgr->灰色编写一个通用方法。其思想是进行一些特定于类型的转换,例如字节的转换,以及其他类型的回退默认转换。 转换方法在Bgr中定义,其中T可以是byte、int、double等 public struct Bgr<T> where T : struct { public T B; public T G; public T R; public unsafe static void Convert(ref Bgr<byte

我正试图为颜色转换Bgr->灰色编写一个通用方法。其思想是进行一些特定于类型的转换,例如字节的转换,以及其他类型的回退默认转换。 转换方法在Bgr中定义,其中T可以是byte、int、double等

public struct Bgr<T>
  where T : struct
{
    public T B;
    public T G;
    public T R;

    public unsafe static void Convert(ref Bgr<byte> bgr, ref Gray<byte> gray)
    {
        //should be executed if T is byte...
    }

    public unsafe static void Convert(ref Bgr<T> bgr, ref Gray<T> gray)
    {
        throw new NotSupportedException();
    }
}
converter类包含ConvertBgrToGray方法,该方法调用Convert方法。如果TSrcDepth是byte,则调用Bgr的Convert,但是调用默认的泛型Convert,它抛出NotSupportedException

public static class BgrGrayConverter
{
    public static void ConvertBgrToGray<TSrcDepth, TDstDepth>(Bgr<TSrcDepth>[,] source, Gray<TDstDepth>[,] dest)
        where TSrcDepth: struct
        where TDstDepth: struct
    {
       ...
       Gray<TSrcDepth> dstVal = default(Gray<TSrcDepth>);
       Bgr<TSrcDepth>.Convert(ref source[j, i], ref dstVal);
       ...
    }
}
如何以适当的方式编写此文件,以便仅在找不到特定类型时调用默认的Convert方法?Bgr必须是一个结构


或者如何以最漂亮的方式重写它,使其工作?

它将只使用接受Bgr的Convert,前提是它可以在编译时确定这一点。见@JohnKoerner谢谢。如何以最漂亮的方式重写这篇文章,使其生效?