有条件可选参数以消除重载
我有以下 C# 方法:
public Vector2 GetVectorToTile(int x, int y)
{
return new Vector2(x * TileWidth, y * TileHeight);
}
public Vector2 GetVectorToTile(Point start)
{
return GetVectorToTile(start.X, start.Y);
}
第二个方法以非常简单的方式重载第一个方法。然而,我真的不喜欢这样的“输入清理程序过载”——我觉得不应该有一个单独的方法来转换每种可能的输入类型。
现在,如果第一个方法的输入是单个 Vector2
而不是两个数字,我可以使用条件参数,这样如果参数是 Point
而不是 Vector2< /code>,它应该首先从
Point
转换为 Vector2
,然后照常进行。
然而,事实并非如此。
所以我的问题是,如何告诉该方法接受“两个整数或一个点”,然后在计算结果之前将后者转换为前者?
我可以在 Matlab 中以一种人为的方式做到这一点,但它看起来与 C# 完全无关:
function result = VectorToTile(varargin)
x = 0;
y = 0;
if size(varargin{1}) == [1, 1]
disp('Assuming Vector input!');
x = varargin{1}{1}.x; % Assuming the "Vector2" equivalent is a struct with .x and .y
y = varargin{1}{1}.y;
else
disp('Assuming integer pair input!');
x = varargin{1}{1};
y = varargin{1}{2};
end
result.x = x * 32; % An example value for TileWidth
result.y = y * 32; % An example value for TileHeight
end
这将与以下两个输入一起使用:
ints{1} = 25;
ints{2} = 125;
VectorToTile(ints);
vect{1}.x = 25;
vect{1}.y = 125;
VectorToTile(vect);
它说明了我想要做的事情,但不幸的是没有真正的 varargin 在 C# 中,也不是所有内容都被视为矩阵。
I have the following C# methods:
public Vector2 GetVectorToTile(int x, int y)
{
return new Vector2(x * TileWidth, y * TileHeight);
}
public Vector2 GetVectorToTile(Point start)
{
return GetVectorToTile(start.X, start.Y);
}
The second method overloads the first in a very simple manner. However, I don't really like such an "input sanitizer overload"- I feel that there shouldn't be a separate method for converting each possible input type.
Now if the input of the first method was a single Vector2
instead of two numbers, I could use conditional arguments such that if the argument is Point
instead of Vector2
, it should first convert from Point
to Vector2
and then proceed as usual.
However, that is not the case.
So my question is, how can I tell the method to accept "EITHER two integers OR a single point", and then convert the latter into the former before computing a result?
I can do this in a contrived manner in Matlab, but it looks completely unrelated to C#:
function result = VectorToTile(varargin)
x = 0;
y = 0;
if size(varargin{1}) == [1, 1]
disp('Assuming Vector input!');
x = varargin{1}{1}.x; % Assuming the "Vector2" equivalent is a struct with .x and .y
y = varargin{1}{1}.y;
else
disp('Assuming integer pair input!');
x = varargin{1}{1};
y = varargin{1}{2};
end
result.x = x * 32; % An example value for TileWidth
result.y = y * 32; % An example value for TileHeight
end
This will work with the following two inputs:
ints{1} = 25;
ints{2} = 125;
VectorToTile(ints);
vect{1}.x = 25;
vect{1}.y = 125;
VectorToTile(vect);
It illustrates what I want to do, but unfortunately there isn't really a varargin
in C#, nor is everything treated as a matrix.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不想显得轻率,但是:
像这样:
您发布的代码不正是您想要的吗?并且以比您发布的 matlab 示例更干净、更类型安全的方式..(恕我直言)
I don't want to seem flippant but:
like this:
isn't the code you posted doing exactly what you want? and in a much cleaner more typesafe way than in the matlab example you posted.. (IMHO)
你可以:
但是同意 Sam Holder 的观点,我认为你在 C# 中所做的很好。上面的内容看起来很混乱,并且可能比您的方法更容易出现错误。例如,如果您提供了全部 3 个参数,哪一个参数优先呢? ETC。
you could:
But agree with Sam Holder, I think what you're doing is fine in C#. The above looks messy and probably more prone to bugs than your approach. E.g., what if you provided all 3 arguments, which would take precedence? etc.
您可以使用参数数组
params
关键字来传入对象数组。例如:
然后您可以检查数组中有多少个并进行相应处理。
但是强类型参数肯定更好吗?
You could use a parameter array
params
keyword to pass in an object array.e.g.:
Then you can check how many is in your array and process accordingly.
But surely strongly typed parameters are better?