【发布时间】:2018-10-26 22:41:47
【问题描述】:
我有这个代码:
public static Tween.TweenExecuter To<T>(ref this T thisArg, T to, float time, EasingType easing = EasingType.Linear,
TweenType type = TweenType.Simple, Func<bool> trigger = null, Action callback = null) where T : struct
{
Tween.TweenElement<T> tween = new Tween.TweenElement<T>()
{
from = thisArg,
Setter = x => thisArg = x,
to = to,
time = time,
easing = easing,
type = type,
Trigger = trigger,
Callback = callback
};
tween = t;
return new Tween.TweenExecuter(tween);
}
Setter 应分配给 Action<T>,但编译器报错:错误 CS1628:无法在匿名方法、lambda 表达式或查询表达式中使用 ref 或 out 参数“thisArg”
否则我该如何使用Action<T>?
编辑:
这是类型声明:
public abstract class BaseTween
{
public float time;
public float currentTime;
public EasingType easing;
public TweenType type;
public bool deleteAtEnd = false;
public Func<bool> Trigger;
public Action Callback;
}
public class TweenElement<T> :BaseTween
{
public Action<T> Setter;
public T from;
public T to;
}
【问题讨论】:
-
为什么是
ref this T thisArg而不是this T thisArg?如果这甚至可行,那么扩展方法是非常不寻常的。 -
@John Wu,它有效。就是这样,因为我需要构造一个 Action
,它会在以后更改 ref thisArg 但方法是扩展方法,所以我需要 ref this。 -
包含
thisArg的原始变量存储在哪里?除了传递变量 byref,也许您可以按值传递包含它的对象。 -
这是什么版本的 C#?我不希望“ref this”能够编译 - 当然对我来说,我得到“参数修饰符 'ref' 不能与 'this' 一起使用”。这是有道理的——毕竟,一个普通的类方法不能改变
this所指的东西,那么为什么扩展应该能够呢? -
@Dylan Nicholson 这是 C# 7.2
标签: c#