问题
I've been working with Vector2's and XNA, and I've come to find that calling the Normalize() member function on a Zero Vector normalizes it to a vector of {NaN, NaN}. This is all well and good, but in my case I'd prefer it instead just leave them as Zero Vectors.
Adding this code to my project enabled a cute extension method:
using ExtensionMethods;
namespace ExtensionMethods
{
public static class MyExtensions
{
public static Vector2 NormalizeOrZero(this Vector2 v2)
{
if (v2 != Vector2.Zero)
v2.Normalize();
return v2;
}
}
}
Unfortunately, this method returns the normalized vector, rather than simply normalizing the vector which I use to invoke this extension method. I'd like to to instead behave as vector2Instance.Normalize() does.
Aside from making this void, how do I adjust this so that the 'v2' is modified? (Essentially, I need access to the 'this' object, or I need 'v2' to be passed by reference.)
Edit:
And yes, I have tried this:
public static void NormalizeOrZero(this Vector2 v2)
{
if (v2 != Vector2.Zero)
v2.Normalize();
}
Doesn't work, v2 is just a variable in the scope of NormalizeOrZero.
回答1:
This doesn't work because Vector 2 is actually a struct. This means it gets passed by value and you can't modify the caller's copy. I think the best you can do is the workaround specified by lomaxxx.
This illustrates why you should generally avoid using structures. See this question for more information. Vector2 violates the guideline that structs should be immutable, but it probably made sense to do so in the context of XNA.
回答2:
Well, if you're really just dying to do this, you could do something like this:
public static void NormalizeOrZero(this Vector2 ignore, ref Vector2 v2)
{
if (v2 != Vector2.Zero)
v2.Normalize();
}
You would call it this way:
v2.NormalizeOrZero(ref v2);
It's mighty ugly, but it'll work, for what it's worth. But at that point you may as well call a static method in the first place.
回答3:
I'm not sure why your second code sample doesn't work but if the first lot of code does what you want you could simply work around it by going:
Vector2 v2 = new Vector2()
v2 = v2.NormalizeOrZero();
回答4:
You would need both the ref
and the this
modifier on the argument, which seems unlikely to work.
来源:https://stackoverflow.com/questions/304041/how-do-i-access-this-from-within-a-c-sharp-extension-method