Ok, maybe this title is a little bit too catchy. However, I simply love the expressiveness of this little piece of code I wrote today.
1: public bool RegisterAll( Assembly assembly, Func<object, bool> isValidMessage )
2: {
3: if( assembly == null )
4: {
5: throw new ArgumentNullException( "assembly" );
6: }
7: if( isValidMessage == null )
8: {
9: throw new ArgumentNullException( "isValidMessage" );
10: }
11: var messages = from t in assembly.GetTypes()
12: where t.HasAttribute<MessageAttribute>() &&
13: isValidMessage( t )
14: select t;
15: return messages.All( Register );
16: }
Comments
would be even nicer and shorter if you used
Guard.ArgumentNotNull(assembly, “assembly”);
😉
I was a little anoyed by the
predicate argument test.
I don’t see why that is there. It is not passed in as an argument, so I can’t really grasp why it triggers a ArgumentException. So it not nice to look @ in insolation.
Benny
@Benny
That’s what happens when you do last time changes without refactoring tools at your hand 😉
It was supposed to be isValidMessage, but I forgot to change it there.
Thanks for noticing.