【问题标题】:function that can take different type of struct C#可以采用不同类型的结构 C# 的函数
【发布时间】:2018-07-17 12:56:05
【问题描述】:

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

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个结构类型和3个变量呢?

标签: c# function struct parameters


【解决方案1】:

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

可以使用反射来做到这一点,但它不会很漂亮,也不会很高效。

您也可以使用接口来做到这一点,但这首先是试图绕过更好地设计代码。

相反,您应该使用 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);
}

【讨论】:

  • 非常感谢您的详细解释!我开始习惯 c#,但有时我的 javascript 行为又让我感到困惑 :)
  • :) Javascript(和其他“无类型”语言)和 C# 完全不同,它们各有优势,但一种优势可能是另一种劣势。通常需要进行思维方式转变或范式转变才能成功地在两者之间切换,祝您过渡好运并不断提出问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多