2013-02-07 30 views
1

我有,我用下面的代碼,我從這個question了創建集合的自定義配置節:嘗試在ConfigurationElementCollection上使用Parallel.ForEach。不能得到它的工作

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?

回答

0

嘗試

config.States.Cast<StateElement>() 

代替

config.States.GetEnumerator() 
+0

這給了我的「錯誤無法從 '方法組' 轉換爲「System.Collections.Generic.IEnumerable 「 – SpaceCowboy74

+0

對不起,我誤解了你的第一個例外。 只是使用config.States,我的作品 如果它仍然無法正常工作給我的代碼和生病會更樂意幫助 –

相關問題