1

この質問から取得した次のコードを使用して作成したコレクションを含むカスタム構成セクションがあります。

public class GenericConfigurationElementCollection<T> : ConfigurationElementCollection, IEnumerable<T> where T : ConfigurationElement, new()
{
    List<T> _elements = new List<T>();

    protected override ConfigurationElement CreateNewElement()
    {
        T newElement = new T();
        _elements.Add(newElement);
        return newElement;
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return _elements.Find(e => e.Equals(element));
    }

    public new IEnumerator<T> GetEnumerator()
    {
        return _elements.GetEnumerator();
    }
}

次のように、プロパティを使用してコレクションを実装しました。

    [ConfigurationProperty("states")]
    [ConfigurationCollection(typeof(StateElement))]
    public GenericConfigurationElementCollection<StateElement> States
    {
        get
        {
            return (GenericConfigurationElementCollection<StateElement>)this["states"];
        }
    }

問題は、Parallel.ForEachを使用してコレクションをループしようとすると次のようになります

Parallel.ForEach<StateElement>(config.States.GetEnumerator(), state=> theState.StateStatus(state));

次のエラーが発生します。

The best overloaded method match for 'System.Threading.Tasks.Parallel.ForEach<States.Configuration.StateElement>(System.Collections.Generic.IEnumerable<States.Configuration.StateElement>, System.Action<States.Configuration.StateElement>)' has some invalid arguments   
Argument 1: cannot convert from 'System.Collections.Generic.IEnumerator<States.Configuration.StateElement>' to 'System.Collections.Generic.IEnumerable<States.Configuration.StateElement>'

最後のものは私を困惑させました。IEnumeratorからIEnumerableに変換できませんか?

4

1 に答える 1

0

試す

config.States.Cast<StateElement>()

それ以外の

config.States.GetEnumerator()
于 2013-02-07T16:42:28.947 に答える