找不到扩展方法?

时间:2018-10-31 13:56:49

标签: c# extension-methods

因此,在环顾四周并尝试自己纠正此问题后,我陷入了困境。我看了以下几篇文章,并确保按照他们所说的那样包括了所有程序集(这是我在来这里之前所做的,但是仔细检查以确保):

仔细检查具有扩展名的文件以及尝试使用扩展名的文件后;还有其他可能的原因为什么找不到扩展方法?

// Extension Class.
using SharpDX;
using SharpDX.Direct2D1;
namespace MyNamespace.Engine {
    public static class Utilities {
        public static Vector3 PointToNDC(this SpriteBatch sb, Size2 screenSize, Point p) {
            float x = 2.0f * p.X / screenSize.Width - 1.0f;
            float y = 1.0f - 2.0f * p.Y / screenSize.Height;
            return new Vector3(x, y, 0);
        }
    }
}

// Usage Class.
using MyNamespace.Engine;
using SharpDX;
using SharpDX.Direct2D1;
namespace MyNamespace.Prefabs {
    public class Sprite {
        public void Draw() {
            SpriteBatch.PointToNDC(new Size2(50, 50), new Point(0, 0));
        }
    }
}

注意

代码中的任何错别字都是实际的错字,而不是代码本身。


更新

正如@Brian Rasmussen在评论中指出的那样,我没有从被扩展对象的实例中调用该方法。我还没有喝咖啡,所以我很抱歉,至少这是一个简单的解决方法!

SpriteBatch sb = new SpriteBatch(...);
sb.PointToNDC(...); // <- Works.

1 个答案:

答案 0 :(得分:2)

要调用PointToNDC作为扩展方法,您需要一个SpriteBatch的实例。

相关问题