gpt4 book ai didi

c# - 反向扩展类

转载 作者:太空宇宙 更新时间:2023-11-03 17:51:49 25 4
gpt4 key购买 nike

我正在尝试创建一个“反向”扩展Rectangle的类。我希望能够将此方法放在类中:

    public Point RightPoint()
{
return new Point(this.X + this.Width, this.Y + this.Height / 2);
}


然后调用 rectangle.RightPoint();并获得返回值。 ( XYWidthHeightRectangle的字段)。

这可能吗?还是我需要制作这些静态方法,然后将它们传递给 Rectangle

最佳答案

我认为您需要extension method

public static Point RightPoint(this Rectangle rectangle)
{
return new Point(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height / 2);
}


上面的代码应该放在 static类中。

然后,您可以在 Rectangle对象上执行此操作:

Rectangle rect = new Rectangle();
Point pointObj = rect.RightPoint();

关于c# - 反向扩展类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23306747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com