誰かが答えを叫ぶ前に、質問を読んでください。
.NET4.0のExpressionVisitorのメソッドの目的は何ですか。
public static ReadOnlyCollection<T> Visit<T>(ReadOnlyCollection<T> nodes, Func<T, T> elementVisitor)
このメソッドの目的についての私の最初の推測は、パラメーターで指定された各ツリーの各ノードにアクセスしnodes
、関数の結果を使用してツリーを書き換えることでしたelementVisitor
。
これは当てはまらないようです。実際、この方法は、私がここで何かを見逃していない限り、何もしないように見えます。
コードでこのメソッドを使用しようとしましたが、期待どおりに機能しなかった場合は、メソッドを反映して次のことがわかりました。
public static ReadOnlyCollection<T> Visit<T>(ReadOnlyCollection<T> nodes, Func<T, T> elementVisitor)
{
T[] list = null;
int index = 0;
int count = nodes.Count;
while (index < count)
{
T objA = elementVisitor(nodes[index]);
if (list != null)
{
list[index] = objA;
}
else if (!object.ReferenceEquals(objA, nodes[index]))
{
list = new T[count];
for (int i = 0; i < index; i++)
{
list[i] = nodes[i];
}
list[index] = objA;
}
index++;
}
if (list == null)
{
return nodes;
}
return new TrueReadOnlyCollection<T>(list);
}
では、誰かが実際にこの方法をどこで使用するのでしょうか。ここで何が欠けていますか?
ありがとう。