IEnumerable's ToDictionary(keyExpression, valueExpression)

In my project I use Entity Framework as my ORM model, using the repository pattern.  Methods in my repository that return a result set return an IQueryable<T>.  I have thee methods annotated as NotNull and ItemNotNull.  From here, if I call ToDictionary(item => item.key, item => item.value) R# knows that item is never null and I do not get a possible null reference exception.

I use a 3rd party library called DelegateDecompiler to allow me to evaluate methods on my entities as part of the query (good example is I can have an active property that is a bool instead of a number as the database uses 0/1 for this).  This is done by calling Decompile() prior to materializing the query (call ToList, ToDictionary, etc).

I have used external annotations to add ItemNotNull and NotNull attributes to the Decompile method so I do not get the possible null reference exception warning on the item within the ToDictionary expressions.  All is good so far.  I don't really want to have to call Decompile() every time I execute a query, so I wrote some extension methods that will call the Decompile method I.E. ToDictionaryDecompiled.  The issue if that I cannot get R# to not complain about a possible null reference exception in the expressions passed to my extension method.

 

Looking at the quick documentation for the regular ToDictionary, I see it is marked as NotNull and Pure, and all the arguments are marked as NotNull and InstantHandle, so I have done with with my extension method, but I still get the possible null reference exception message.

 

What am I missing?

 

~~~Extension method usage example with R# warning~~~

~~~Extension method declaration~~~

~~~Quick documentation for Linq's ToDictionary method~~~

 

One more note, if I chain Decompile().toDictionary() directly, I do not get the warning.

0
1 comment

ReSharper has special handlers for extension methods from System.Linq.Enumerable which infers nullability of lambda's arguments using "ItemNotNull/ItemCanBeNull" annotations of pre-chained methods. Unfortunately, currently there is no way to have ReSharper  handle user's methods in the same way

0

Please sign in to leave a comment.