c# - Parallel.ForEach over AuthorizationRuleCollection -
hopefully point out doing wrong here. have process reads in file access rules , trying run in parallel.
here code have far:
public list<folderaccessrule> getsecuritygroupsforfolder(long importid, string subdirectory, bool includeinherited) { concurrentqueue<folderaccessrule> groups = new concurrentqueue<folderaccessrule>(); concurrentqueue<exception> exceptions = new concurrentqueue<exception>(); directoryinfo dinfo = new directoryinfo(subdirectory); directorysecurity dsecurity = dinfo.getaccesscontrol(); authorizationrulecollection authorizationrulecollection = dsecurity.getaccessrules(true, includeinherited, typeof(ntaccount)); parallel.foreach( authorizationrulecollection, fsar => { try { folderaccessrule group = this.getgroup(fsar); if (group != null) { groups.enqueue(group); } } catch (exception e) { exceptions.enqueue(e); } }); foreach (exception e in exceptions) { string message = string.concat(e.gettype(), "exception getting directory info ", subdirectory); this.recordexception(message, subdirectory, e); } return groups.tolist(); }
which (to untrained eye), looks should work. parallel.foreach
not compile gives error:
the type arguments method 'system.threading.tasks.parallel.foreach<tsource> (system.collections.generic.ienumerable<tsource>, system.action<tsource>)' cannot inferred usage. try specifying type arguments explicitly.
i have tried changing offending line to:
parallel.foreach<filesystemaccessrule>( authorizationrulecollection, fsar =>
but still no joy.
so, doing wrong? in advance.
pass authorizationrulecollection.oftype<authorizationrule>()
parallel.foreach
, eg:
var typedcolletion=authorizationrulecollection.oftype<authorizationrule>(); parallel.foreach(typedcolletion);
the error caused because authorizationrulecollection
doesn't implement ienumerable<t>
ienumerable
, elements object
. common pattern older (pre - .net 2.0) collections, when generics weren't available.
newer .net classes return/expect generic collections. calling oftype<t>()
or cast<t>()
cast source ienumerable
ienumerable<t>
.
the difference oftype<t>()
filter out incompatible elements, while cast<t>()
throw if incompatible elements found.
the code behind both methods isn't magic, in reference source it's iterator on source ienumerable
:
public static ienumerable<tresult> oftype<tresult>(this ienumerable source) { if (source == null) throw error.argumentnull("source"); return oftypeiterator<tresult>(source); } static ienumerable<tresult> oftypeiterator<tresult>(ienumerable source) { foreach (object obj in source) { if (obj tresult) yield return (tresult)obj; } }
Comments
Post a Comment