call graph IEnumerator
<p>I have to implement a call graph for expressions like <code>Id = Id(Param);</code> and that wasn't a problem. </p>

<p>Now I have to implement an enumerator which lists one at a time all topological orderings among the calls that satisfy the order of dependencies. </p>

<p>And here's the trouble.</p>

<p>This is a simple node for the call graph:</p>

<pre><code>class CallGraphNode
{
    private string name;
    public List&lt;CallGraphNode&gt; dependents = new List&lt;CallGraphNode&gt;();
    public int dependencies;
    private bool executed = false;
    public bool Executable { get { return dependencies == 0; } }
    public bool Executed { get { return executed; } set { executed = value; } }

    public CallGraphNode(string name)
    {
        this.name = name;
        dependencies = 0;    
    }

    public override string ToString()
    {
        return name;
    }

    public void AddDependent(CallGraphNode n)
    {
        dependents.Add(n);
    }          
}
</code></pre>

<p>And this is the call graph class itself:</p>

<pre><code>class CallGraph : IEnumerable&lt;List&lt;CallGraphNode&gt;&gt;
{
    public List&lt;CallGraphNode&gt; nodes = new List&lt;CallGraphNode&gt;();

    public void AddNode(CallGraphNode n)
    {
        nodes.Add(n);
    }

    public static void Show(IEnumerable&lt;CallGraphNode&gt; n)
    {
        foreach (CallGraphNode node in n)
        {
            Console.Write("{0} ", node);
        }
        Console.WriteLine();
    }

    static IEnumerable&lt;List&lt;CallGraphNode&gt;&gt; EnumerateFunctions(List&lt;CallGraphNode&gt; executable, List&lt;CallGraphNode&gt; res)
    {
        if (executable.Count == 0)
            yield return res;
        else foreach (CallGraphNode n in executable)
            {
                if (!n.Executed)
                    res.Add(n);
                List&lt;CallGraphNode&gt; next_executable = new List&lt;CallGraphNode&gt;(executable);
                executable.Remove(n);
                foreach (CallGraphNode m in n.dependents)
                    if (--m.dependencies == 0)
                        next_executable.Add(m);
                foreach (List&lt;CallGraphNode&gt; others in EnumerateFunctions(next_executable, res))
                    yield return others;
                foreach (CallGraphNode m in n.dependents)
                    m.dependencies++;
                if (!n.Executed)
                    res.Remove(n);
            }
    }

    IEnumerator&lt;List&lt;CallGraphNode&gt;&gt; IEnumerable&lt;List&lt;CallGraphNode&gt;&gt;.GetEnumerator()
    {
        List&lt;CallGraphNode&gt; executable = new List&lt;CallGraphNode&gt;();
        foreach (CallGraphNode n in nodes)
            if (n.Executable || n.Executed)
                executable.Add(n);
        List&lt;CallGraphNode&gt; output = new List&lt;CallGraphNode&gt;();
        foreach (List&lt;CallGraphNode&gt; list in EnumerateFunctions(executable, output))
            yield return list;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    { throw new NotImplementedException(); }
}
</code></pre>

<p>Now, the problem is it just won't work. When I try to create an <code>IEnumerator</code> and assign it the <code>GetEnumerator()</code> return value, I get a casting error and that's honestly what I expected when trying to do so:</p>

<pre><code>IEnumerator&lt;List&lt;CallGraphNode&gt;&gt; lt = cg.GetEnumerator();
</code></pre>

<p>Then I've tried:</p>

<pre><code>System.Collections.Generic.List&lt;CallGraphNode&gt;.Enumerator en = cg.nodes.GetEnumerator();
</code></pre>

<p>This works, but the method <code>EnumerateFunctions</code> is never called and the enumerator just contains the original list of graph nodes.</p>

<p>Any ideas?</p>
