如何从C#扩展方法中访问“this”?

时间:2022-05-02 22:50:27

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.

我一直在使用Vector2和XNA,我发现调用Zero Vector上的Normalize()成员函数将其规范化为{NaN,NaN}的向量。这一切都很好,但在我的情况下,我更喜欢它,而只是留下它们作为零矢量。

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.

不幸的是,这个方法返回规范化的向量,而不是简单地规范化我用来调用这个扩展方法的向量。我想改为像vector2Instance.Normalize()那样行事。

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.)

除了使这个空白之外,我该如何调整它以便修改'v2'? (基本上,我需要访问'this'对象,或者我需要通过引用传递'v2'。)

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.

不起作用,v2只是NormalizeOrZero范围内的变量。

4 个解决方案

#1


3  

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.

这不起作用,因为Vector 2实际上是一个结构。这意味着它按值传递,您无法修改调用者的副本。我认为你能做的最好的是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.

这说明了为什么通常应该避免使用结构。有关更多信息,请参阅此问题。 Vector2违反了结构应该是不可变的准则,但在XNA的上下文中这样做可能是有意义的。

#2


1  

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


0  

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


0  

You would need both the ref and the this modifier on the argument, which seems unlikely to work.

你需要在参数上使用ref和this修饰符,这似乎不太可行。

#1


3  

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.

这不起作用,因为Vector 2实际上是一个结构。这意味着它按值传递,您无法修改调用者的副本。我认为你能做的最好的是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.

这说明了为什么通常应该避免使用结构。有关更多信息,请参阅此问题。 Vector2违反了结构应该是不可变的准则,但在XNA的上下文中这样做可能是有意义的。

#2


1  

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


0  

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


0  

You would need both the ref and the this modifier on the argument, which seems unlikely to work.

你需要在参数上使用ref和this修饰符,这似乎不太可行。