代码之家  ›  专栏  ›  技术社区  ›  FutureCake

可以采用不同类型的结构C的函数#

  •  0
  • FutureCake  · 技术社区  · 8 年前

    我有一个函数需要特定的数据类型作为参数。函数如下所示:

    public static Color get_axis_loc_color(colormap axis, float location){
        var difRed = axis.r_end - axis.r_start;
        var difGreen = axis.g_end - axis.g_start;
        var difBlue = axis.b_end - axis.b_start;
    
        difRed = (int)(difRed * location) + axis.r_start;
        difGreen = (int)(difGreen * location) + axis.g_start;
        difBlue = (int)(difBlue * location) + axis.b_start;
    
        return new Color(a: 1, r: difRed, g: difGreen, b: difBlue);
    }
    

    现在我有了一个结构,它包含颜色映射数据,如下所示:

    public struct color_map  
    {
        public struct x 
        {
            public static readonly int r_start =  255;
            public static readonly int g_start =  255;
            public static readonly int b_start =   255;
            public static readonly int r_end =  0;
            public static readonly int g_end =  0;
            public static readonly int b_end =  255;
        }
    
        public struct y
        {
            public static readonly int r_start = 255;
            public static readonly int g_start = 255;
            public static readonly int b_start =  255;
            public static readonly int r_end = 255;
            public static readonly int g_end = 0;
            public static readonly int b_end = 0;
        }
    
        public struct z
        {
            public static readonly int r_start = 103;
            public static readonly int g_start = 190;
            public static readonly int b_start = 155;
            public static readonly int r_end = 0;
            public static readonly int g_end = 150;
            public static readonly int b_end = 0;
        }
    }
    

    现在,当我调用函数时,我需要能够为 colormap axis 参数:
    colormap.x 或 colormap.y 或 colormap.z

    但我不能这样做,因为类型不匹配。我该怎么做呢?

    如果有任何不清楚的地方,请告诉我,以便我可以澄清。
    事先谢谢!

    1 回复  |  直到 8 年前
        1
  •  4
  •   Lasse V. Karlsen    8 年前

    在这里设计代码时,您走错了路。任何声称 只是 回答你的问题不会给你正确的方向。

    你 可以 用反射来做,但不会很漂亮,也不会被表演。

    您也可以使用接口来完成这项工作,但首先,这将试图绕过更好地设计代码。

    相反,您应该使用1类型和3 变量 .

    这里,让我演示一下:

    public struct color_map
    {
        private color_map(int r1, int g1, int b1, int r2, int g2, int b2)
        {
            r_start = r1;
            g_start = g1;
            b_start = b1;
            r_end = r2;
            g_end = g2;
            b_end = b2;
        }
    
        public int r_start { get; }
        public int g_start { get; }
        public int b_start { get; }
        public int r_end { get; }
        public int g_end { get; }
        public int b_end { get; }
    
        public static readonly color_map x = new color_map(255, 255, 255, 0, 0, 255);
        public static readonly color_map y = new color_map(255, 255, 255, 255, 0, 0);
        public static readonly color_map z = new color_map(103, 190, 155, 0, 150, 0);
    }
    

    这将允许您传入类型为的参数 color_map 并访问属性。

    你的 get_axis_color 然后可以这样声明方法(我重写它以使用 System.Drawing.Color 但是请注意,我所做的另一个更改是将其设为A型 彩色地图 参数,而不是 colormap ,与上述结构一致):

    public static Color get_axis_loc_color(color_map axis, float location)
    {
        var difRed = axis.r_end - axis.r_start;
        var difGreen = axis.g_end - axis.g_start;
        var difBlue = axis.b_end - axis.b_start;
    
        difRed = (int)(difRed * location) + axis.r_start;
        difGreen = (int)(difGreen * location) + axis.g_start;
        difBlue = (int)(difBlue * location) + axis.b_start;
    
        return Color.FromArgb(alpha: 1, red: difRed, green: difGreen, blue: difBlue);
    }