简体   繁体   English

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

[英]How do I access 'this' from within a C# extension method?

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}. 我一直在使用Vector2和XNA,我发现调用Zero Vector上的Normalize()成员函数将其规范化为{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. 我想改为表现为vector2Instance .Normalize()。

Aside from making this void, how do I adjust this so that the 'v2' is modified? 除了使这个空白之外,我该如何调整它以便修改'v2'? (Essentially, I need access to the 'this' object, or I need 'v2' to be passed by reference.) (基本上,我需要访问'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范围内的变量。

This doesn't work because Vector 2 is actually a struct . 这不起作用,因为Vector 2 实际上是一个结构 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. 我认为你能做的最好的是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的上下文中这样做可能是有意义的。

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. 但是在那时你也可以首先称之为静态方法。

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();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从同一 class C# 中的另一种方法访问一种方法的变量? - How do I access the variable of one method from another method within the same class C#? 如何从C#中的方法访问构造函数中的变量 - How do I access a variable in a constructor from a method in C# 如何从 C# 控制台程序中调用扩展方法? - How can I call an extension method from within a C# Console program? 如何在不打开的情况下从F#调用C#扩展方法? - How do I call a C# extension method from F#, without open? 如何从Action中捕获异常 <T> 或在C#中包含匿名方法? - How do I catch exceptions from within Action<T> or containing an anonymous method in c#? 如何在C ++ / CLI中声明一个在C#中被视为扩展方法的方法? - How do I declare a method in C++/CLI that will be seen as an extension method in C#? 如何从C#访问ac $资源上的文件 - How do I access files on a c$ resource from C# 如何从C#中的另一个方法访问一个方法中的列表项 - how to access a list item within a method from another method in C# 如何创建使用类似于在C#中使用的配置文件的扩展方法 - How do I create a extension method that uses a profile similar to using in C# 如何从 C# 访问关系 SQL 数据库? - How do I access a relational SQL database from C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM