为什么我不能使用 stopwatch.Restart()?
Why am I not able to use stopwatch.Restart()?
我试图在秒表实例上调用 Restart()
,但在尝试调用它时出现以下错误:
Assets/Scripts/Controls/SuperTouch.cs(22,59): error CS1061: Type
System.Diagnostics.Stopwatch' does not contain a definition for
Restart' and no extension method Restart' of type
System.Diagnostics.Stopwatch' could be found (are you missing a using
directive or an assembly reference?)
这是我的代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace Controls
{
public class SuperTouch
{
public Vector2 position { get { return points [points.Count - 1]; } }
public float duration { get { return (float)stopwatch.ElapsedMilliseconds; } }
public float distance;
public List<Vector2> points = new List<Vector2> ();
public Stopwatch stopwatch = new Stopwatch ();
public void Reset ()
{
points.Clear ();
distance = 0;
stopwatch.Restart ();
}
}
}
我猜您使用的是 4.0 之前的框架,这意味着您必须使用 Reset
和 Start
而不是 Restart
。
我猜您正在使用 .Net Framework 3.5
或以下 Stopwatch
的 Restart
方法不存在的地方。
如果你想复制相同的行为,你可以这样做。
Stopwatch watch = new Stopwatch();
watch.Start();
// do some things here
// output the elapse if needed
watch = Stopwatch.StartNew(); // creates a new Stopwatch instance
// and starts it upon creation
.Net Framework 2.0
上已存在 StartNew 静态方法
有关 StartNew 方法的更多详细信息:Stopwatch.StartNew Method
或者,您可以自己创建一个扩展方法。
这是模型和用法。
public static class ExtensionMethods
{
public static void Restart(this Stopwatch watch)
{
watch.Stop();
watch.Start();
}
}
消费喜欢
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Restart(); // an extension method
}
}
Unity 引擎使用 .NET 2.0 的一个子集。正如其他人所说,Restart
是在 .NET 4.0 中添加的。 This useful page 显示了您可以安全使用的所有 .NET 函数。如您所见,存在 Start
和 Reset
。
不要调用多个方法(容易出现人为错误),而是使用扩展方法。
public static class StopwatchExtensions
{
/// <summary>
/// Support for .NET Framework <= 3.5
/// </summary>
/// <param name="sw"></param>
public static void Restart(this Stopwatch sw)
{
sw.Stop();
sw.Reset();
sw.Start();
}
}
我试图在秒表实例上调用 Restart()
,但在尝试调用它时出现以下错误:
Assets/Scripts/Controls/SuperTouch.cs(22,59): error CS1061: Type
System.Diagnostics.Stopwatch' does not contain a definition for
Restart' and no extension methodRestart' of type
System.Diagnostics.Stopwatch' could be found (are you missing a using directive or an assembly reference?)
这是我的代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace Controls
{
public class SuperTouch
{
public Vector2 position { get { return points [points.Count - 1]; } }
public float duration { get { return (float)stopwatch.ElapsedMilliseconds; } }
public float distance;
public List<Vector2> points = new List<Vector2> ();
public Stopwatch stopwatch = new Stopwatch ();
public void Reset ()
{
points.Clear ();
distance = 0;
stopwatch.Restart ();
}
}
}
我猜您使用的是 4.0 之前的框架,这意味着您必须使用 Reset
和 Start
而不是 Restart
。
我猜您正在使用 .Net Framework 3.5
或以下 Stopwatch
的 Restart
方法不存在的地方。
如果你想复制相同的行为,你可以这样做。
Stopwatch watch = new Stopwatch();
watch.Start();
// do some things here
// output the elapse if needed
watch = Stopwatch.StartNew(); // creates a new Stopwatch instance
// and starts it upon creation
.Net Framework 2.0
有关 StartNew 方法的更多详细信息:Stopwatch.StartNew Method
或者,您可以自己创建一个扩展方法。
这是模型和用法。
public static class ExtensionMethods
{
public static void Restart(this Stopwatch watch)
{
watch.Stop();
watch.Start();
}
}
消费喜欢
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Restart(); // an extension method
}
}
Unity 引擎使用 .NET 2.0 的一个子集。正如其他人所说,Restart
是在 .NET 4.0 中添加的。 This useful page 显示了您可以安全使用的所有 .NET 函数。如您所见,存在 Start
和 Reset
。
不要调用多个方法(容易出现人为错误),而是使用扩展方法。
public static class StopwatchExtensions
{
/// <summary>
/// Support for .NET Framework <= 3.5
/// </summary>
/// <param name="sw"></param>
public static void Restart(this Stopwatch sw)
{
sw.Stop();
sw.Reset();
sw.Start();
}
}