【发布时间】: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.x 或 colormap.y 或 colormap.z
但我不能这样做,因为类型不匹配。我该怎么做呢?
如果有任何不清楚的地方,请告诉我,以便我澄清。
提前致谢!
【问题讨论】:
-
即使您设法创建了方法,这也不起作用,因为静态字段的使用在编译时被编译到代码中,您必须对所有内容使用反射。但是,既然所有的结构在结构中都是相同的,为什么不声明1个结构类型和3个变量呢?
标签: c# function struct parameters