[英]How to use a fragment of a method many times in a method?
我已经搜索了大约一个小时,但没有发现任何结果:在对某些方法进行编程时,我经常会遇到一种情况,即某个方法需要重复一些片段。 我知道仅复制这些内容是不好的。
我想要做的是将一些代码放入例如Action<>
或Func<>
并在代码的不同位置使用它们。 问题是,我不能使用任何this.
属性。 Visual Studio建议声明类似var thisForAction = this;
东西var thisForAction = this;
并使用此局部变量。 真的是唯一的方法吗?
提前致谢 :)
//编辑:到目前为止,这是我的解决方案
struct TransformationMod
{
public Nullable<float> Rotation;
public Nullable<Vector2D> Position;
public Nullable<Vector2> Scale;
public struct Origin
{
public Vector2D Point; // if not Absolute, then 0 <= x,y <= 1
public bool HasAbsoluteCoordinates;
}
public Nullable<Origin> RotationOrigin;
public bool Absolute;
public Transformation Perform(Transformation On)
{
Transformation result = On;
float rotationBefore = result.Rotation;
Action<Origin?> HandleRotationOrigin =
delegate(Origin? RotationOrigin)
{
if (RotationOrigin.HasValue)
{
// We need to change position to a new one - produced by rotation around a spacified origin
// http://www.gamefromscratch.com/post/2012/11/24/GameDev-math-recipes-Rotating-one-point-around-another-point.aspx
// counting center of rotation
Vector2D center;
center = RotationOrigin.Value.Point;
if (!RotationOrigin.Value.HasAbsoluteCoordinates)
{
// check with lines below if() and the link provided. Also from stackoverflow:
/*
float s = sin(angle);
float c = cos(angle);
// translate point back to origin:
p.x -= cx;
p.y -= cy;
// rotate point
float xnew = p.x * c - p.y * s;
float ynew = p.x * s + p.y * c;
// translate point back:
p.x = xnew + cx;
p.y = ynew + cy;
*/
// we rotate an ABSOLUTE angle, not the difference
double sinx = Math.Sin(rotationBefore);
double cosx = Math.Cos(rotationBefore);
center.X *= result.Size.X;
center.Y *= result.Size.Y;
double tmpCenX = cosx * center.X - sinx * center.Y;
center.Y = sinx * center.X + cosx * center.Y;
center.X = tmpCenX;
center += result.Position;
}
// counting rotation
double cos, sin;
cos = Math.Cos(result.Rotation - rotationBefore);
sin = Math.Sin(result.Rotation - rotationBefore);
result.Position.X -= center.X;
result.Position.Y -= center.Y;
double tmpPosX = cos * result.Position.X - sin * result.Position.Y;
result.Position.Y = sin * result.Position.X + cos * result.Position.Y;
result.Position.X = tmpPosX;
result.Position.X += center.X;
result.Position.Y += center.Y;
}
};
if (Absolute)
{
if (Rotation.HasValue)
{
result.Rotation = Rotation.Value;
HandleRotationOrigin(RotationOrigin);
}
if (Position.HasValue)
result.Position = Position.Value;
if (Scale.HasValue)
result.Scale = Scale.Value;
}
else
{
if (Rotation.HasValue)
{
result.Rotation += Rotation.Value;
HandleRotationOrigin(RotationOrigin);
}
if (Position.HasValue)
result.Position += Position.Value;
if (Scale.HasValue)
result.Scale += Scale.Value;
}
return result;
}
它(几乎)是在结构(值类型)中的唯一方法,如果它是一个类,则您的代码将进行编译。
还要注意,AS TransformationMod是一种struct / value类型,如果您将其传递给您,将对其进行复制,因此您的匿名方法(甚至是命名方法)将在副本上进行操作,为避免这种情况,您需要将其与ref一起传递,但请参阅Action <ref T1,T2>的委托并阅读有关拳击的信息
我建议您无论如何都希望这是一堂课。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.