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

时间:2018-07-17 12:56:05

标签: c# function struct parameters

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

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.xcolormap.ycolormap.z

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

如果不清楚,请告诉我,以便我澄清。
预先感谢!

1 个答案:

答案 0 :(得分:4)

在这里设计代码时,您走错了路。任何试图公正回答您问题的答案都不会给您正确的方向。

可以使用反射来做到这一点,但它不会变得漂亮,也不会表现出色。

您也可以使用接口来完成此操作,但这首先是为了规避代码的设计。

相反,您应该使用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,但是请注意,我所做的唯一更改是使它键入一个color_map参数,而不是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);
}