【问题标题】:Passing a value by ref to an IENumerator function?通过 ref 将值传递给 IENumerator 函数?
【发布时间】:2012-07-29 23:04:15
【问题描述】:

为了澄清我在 Unity3D 中执行此操作,这可能重要也可能不重要?

我试图弄清楚是否可以通过 ref 将值传递给不产生的 IEnumerator 函数。如果我尝试使用产生的函数来执行此操作,VS2010 会抱怨(“迭代器不能有 ref 或 out 参数”),但是,如果我使用类似的 IEnumerator 函数来调用产生函数,但不会自行产生,错误消失了,事情似乎开始工作了。我正在尝试找出我是否处于意外行为状态,或者这是否是正常行为。

这是我正在做的一个例子:

IEnumerator Wrapper(ref int value)
{
    int tmp = ++value;    // This is the ONLY place I want the value
    return Foo(tmp);      // of the ref parameter to change!  
}                         // I do _NOT_ want the value of the ref
                          // parameter to change in Foo()!
IENumerator Foo(int value)
{
    // blah blah
    someFunc(value);
    someSlowFunc();
    yield return null;
    yield return null;
}

【问题讨论】:

  • 好吧,不要指望value 在初始增量后会改变。
  • 我只希望在 Wrapper 中增加它时改变它。在我的代码中,有一个值需要在调用代码之前增加一次且仅一次,因此我想将其包装起来,这样我就不会在某些时候不小心忘记这样做并引入难以追踪的错误。 (将问题更新为在这方面更清晰,谢谢!)
  • 请注意,您现在可以非常轻松地...answers.unity3d.com/answers/551381/view.html

标签: c# unity3d ienumerator


【解决方案1】:

看起来不错。顶部函数只返回一个 IEnumerator - 但在其他方面是一个普通函数。底部函数 是 一个 IEnumerator [由编译器转换为一个时髦的类],因此不能有 ref 值。

top 函数可以这样写:

 void Wrapper(ref int value, out IEnumerator coroutine)
 {
     int tmp = ++value;
     coroutine = Foo(tmp);
 }

这有点混乱 - 但它显示了这是一个处理两条数据的正常函数。一个通过引用传递的 int,以及一个返回的 IEnumerator [只是一个类] [在本例中使用 out]。


补充:这就是幕后工作的方式:

    static void Main(string[] args)
    {
        //Lets get the 'IEnumerable Class' that RandomNum gets compiled down into.
        var IEnumeratorClass = RandomNum(10, 10);

        //All an IEnumerable is is a class with 'GetEnumerator'... so lets get it!
        var IEnumerableClass = IEnumeratorClass.GetEnumerator();

        //It can be used like so:
        while (IEnumerableClass.MoveNext())
        {
            Console.WriteLine(IEnumerableClass.Current);
        }

        Console.WriteLine(new String('-', 10));

        //Of course, that's a lot of code for a simple job.
        //Luckily - there's some nice built in functionality to make use of this.
        //This is the same as above, but much shorter
        foreach (var random in RandomNum(10, 10)) Console.WriteLine(random);

        Console.WriteLine(new String('-', 10));

        //These simple concepts are behind Unity3D coroutines, and Linq [which uses chaining extensively]
        Enumerable.Range(0, 100).Where(x => x % 2 == 0).Take(5).ToList().ForEach(Console.WriteLine);

        Console.ReadLine();
    }

    static Random rnd = new Random();
    static IEnumerable<int> RandomNum(int max, int count)
    {
        for (int i = 0; i < count; i++) yield return rnd.Next(i);
    }

    //This is an example of what the compiler generates for RandomNum, see how boring it is?
    public class RandomNumIEnumerableCompiled : IEnumerable<int>
    {
        int max, count;
        Random _rnd;
        public RandomNumIEnumerableCompiled(int max, int count)
        {
            this.max = max;
            this.count = count;
            _rnd = rnd;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return new RandomNumIEnumeratorCompiled(max, count, rnd);
        }

        IEnumerator<int> IEnumerable<int>.GetEnumerator()
        {
            return new RandomNumIEnumeratorCompiled(max, count, rnd);
        }

    }
    public class RandomNumIEnumeratorCompiled : IEnumerator<int>
    {
        int max, count;
        Random _rnd;
        int current;
        int currentCount = 0;
        public RandomNumIEnumeratorCompiled(int max, int count, Random rnd)
        {
            this.max = max;
            this.count = count;
            _rnd = rnd;
        }

        int IEnumerator<int>.Current { get { return current; } }

        object IEnumerator.Current { get { return current; } }

        public bool MoveNext()
        {
            if (currentCount < count)
            {
                currentCount++;
                current = rnd.Next(max);
                return true;
            }
            return false;
        }

        public void Reset() { currentCount = 0; }
        public void Dispose() { }
    }

【讨论】:

  • 在我的真实代码中,我正在加载网络级别,我可以为所有网络视图对象提供一个前缀值,每次加载新级别时都应该递增该值。这是为了防止来自先前级别的旧调用影响新加载的级别。 每次调用这个 Foo() 函数时都会发生这种情况,如果我不这样做,将会引入难以跟踪的错误。这就是为什么我想确保它总是发生,而不是在调用函数时不得不依靠记住手动执行增量。
  • 另外,感谢您的回答。现在说得通了(我正在阅读更多关于 IEnumerator 函数的内容,但在阅读您的答案之前我并不确定它是如何工作的,所以,谢谢!)
  • 呵呵呵呵,听起来不错 :) 刚刚做了一个小编辑,以帮助澄清发生了什么。 IEnumerator 的 [和 IEnumerable 的] 有点令人困惑,因为背景魔法,但是一旦你知道它们是如何工作的就会非常有用:)
  • 最后一件事:这无关紧要,但也许有人会发现它很有用,在 unity3d 中,您不能通过在某些内置函数(例如 Start() 和 FixedUpdate())中调用协程函数来启动协程函数- 所以你必须使用他们的函数“StartCoroutine()”——这需要包装函数返回一个 IEnumerator。这可能是首先引起我很多困惑的原因!
  • 我是 Unity 开发人员 - 所以我明白。您所看到的与统一无关 - IEnumerator 只返回一个 IEnumerator。就像 List 类型的函数只返回一个 List 一样——尽管在前一种情况下编译器会“自动”为您创建 IEnumerator 类。 StartCoroutine 是一个使用 IEnumerator 并让事情发生的函数。
猜你喜欢
  • 2017-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
相关资源
最近更新 更多