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

C# 可以采用不同类型结构C的函数#,c#,function,struct,parameters,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 =

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

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);
}
现在我有了一个包含colormap数据的结构,如下所示:

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轴
参数传递以下变量:
colormap.x
colormap.y
colormap.z

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

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

提前谢谢

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

你可以用反射来做,但它不会很漂亮,也不会很好的表现

您也可以使用接口来实现这一点,但这首先是为了避免更好地设计代码

相反,您应该使用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\u map
的参数并访问属性

然后可以这样声明
get\u axis\u color
方法(我将其重写为使用
System.Drawing.color
,但请注意,我所做的唯一其他更改是使其键入a
color\u map
参数,而不是
colormap
,以符合上面的结构):


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

你可以用反射来做,但它不会很漂亮,也不会很好的表现

您也可以使用接口来实现这一点,但这首先是为了避免更好地设计代码

相反,您应该使用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\u map
的参数并访问属性

然后可以这样声明
get\u axis\u color
方法(我将其重写为使用
System.Drawing.color
,但请注意,我所做的唯一其他更改是使其键入a
color\u map
参数,而不是
colormap
,以符合上面的结构):


即使您设法创建了您的方法,这也不起作用,因为静态字段的使用是在编译时编译到代码中的,所以您必须对所有内容使用反射。然而,由于所有结构在结构上都是相同的,为什么不声明1个结构类型和3个变量呢?即使您设法创建了方法,这也行不通,因为静态字段的使用在编译时被编译到代码中,所以您必须对所有内容使用反射。但是,既然所有结构在结构上都是相同的,为什么不声明1个结构类型和3个变量呢?非常感谢您的详细解释!我开始习惯c#但有时我的javascript行为会让我感到困惑:):)javascript(和其他“无类型”语言)和c#完全不同,它们各有长处,但一个方面的长处可能是另一个方面的弱点。通常需要心态转变或范式转变才能成功地在两者之间切换,祝过渡顺利,让他们的问题不断出现。非常感谢您的详细解释!我开始习惯c#但有时我的javascript行为会让我感到困惑:):)javascript(和其他“无类型”语言)和c#完全不同,它们各有长处,但一个方面的长处可能是另一个方面的弱点。通常需要心态转变或范式转变,才能成功地在两者之间切换,祝过渡顺利,让他们的问题不断出现。