Skip to content

Commit 3703828

Browse files
authored
Fix nullable issues (#636)
* DefaultDynamicLinqCustomTypeProvider * . * . * . * . * x * . * . * .
1 parent abb8c03 commit 3703828

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1269
-1289
lines changed
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
22
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=EF/@EntryIndexedValue">EF</s:String>
3+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IL/@EntryIndexedValue">IL</s:String>
34
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UTC/@EntryIndexedValue">UTC</s:String>
45
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=WASM/@EntryIndexedValue">WASM</s:String>
5-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Formattable/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
6+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Formattable/@EntryIndexedValue">True</s:Boolean>
7+
<s:Boolean x:Key="/Default/UserDictionary/Words/=renamer/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Copyright>Copyright © ZZZ Projects</Copyright>
88
<DefaultLanguage>en-us</DefaultLanguage>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
10-
<LangVersion>latest</LangVersion>
10+
<LangVersion>10</LangVersion>
1111
<Nullable>enable</Nullable>
1212
<PackageIcon>logo.png</PackageIcon>
1313
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>

src/Microsoft.EntityFrameworkCore.DynamicLinq.EFCore3/EFDynamicQueryableWithFormattableStringExtensions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Reflection;
1414
using System.Threading;
1515
using System.Threading.Tasks;
16+
#pragma warning disable CS1591
1617
using JetBrains.Annotations;
1718
using System.Text.RegularExpressions;
1819

src/System.Linq.Dynamic.Core/Compatibility/ExpressionVisitor.cs

+26-24
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ protected ExpressionVisitor()
1919
protected virtual Expression Visit(Expression exp)
2020
{
2121
if (exp == null)
22+
{
2223
return exp;
24+
}
2325

2426
switch (exp.NodeType)
2527
{
@@ -103,7 +105,7 @@ protected virtual MemberBinding VisitBinding(MemberBinding binding)
103105

104106
protected virtual ElementInit VisitElementInitializer(ElementInit initializer)
105107
{
106-
ReadOnlyCollection<Expression> arguments = this.VisitExpressionList(initializer.Arguments);
108+
ReadOnlyCollection<Expression> arguments = VisitExpressionList(initializer.Arguments);
107109
if (arguments != initializer.Arguments)
108110
{
109111
return Expression.ElementInit(initializer.AddMethod, arguments);
@@ -127,13 +129,15 @@ protected virtual Expression VisitBinary(BinaryExpression b)
127129
{
128130
Expression left = Visit(b.Left);
129131
Expression right = Visit(b.Right);
130-
Expression conversion = Visit(b.Conversion);
132+
Expression conversion = Visit(b.Conversion!);
131133
if (left != b.Left || right != b.Right || conversion != b.Conversion)
132134
{
133135
if (b.NodeType == ExpressionType.Coalesce && b.Conversion != null)
136+
{
134137
return Expression.Coalesce(left, right, conversion as LambdaExpression);
135-
else
136-
return Expression.MakeBinary(b.NodeType, left, right, b.IsLiftedToNull, b.Method);
138+
}
139+
140+
return Expression.MakeBinary(b.NodeType, left, right, b.IsLiftedToNull, b.Method);
137141
}
138142

139143
return b;
@@ -186,8 +190,8 @@ protected virtual Expression VisitMemberAccess(MemberExpression m)
186190

187191
protected virtual Expression VisitMethodCall(MethodCallExpression m)
188192
{
189-
Expression obj = Visit(m.Object);
190-
IEnumerable<Expression> args = this.VisitExpressionList(m.Arguments);
193+
Expression obj = Visit(m.Object!);
194+
IEnumerable<Expression> args = VisitExpressionList(m.Arguments);
191195
if (obj != m.Object || args != m.Arguments)
192196
{
193197
return Expression.Call(obj, m.Method, args);
@@ -198,10 +202,10 @@ protected virtual Expression VisitMethodCall(MethodCallExpression m)
198202

199203
protected virtual ReadOnlyCollection<Expression> VisitExpressionList(ReadOnlyCollection<Expression> original)
200204
{
201-
List<Expression> list = null;
205+
List<Expression>? list = null;
202206
for (int i = 0, n = original.Count; i < n; i++)
203207
{
204-
Expression p = this.Visit(original[i]);
208+
Expression p = Visit(original[i]);
205209
if (list != null)
206210
{
207211
list.Add(p);
@@ -239,7 +243,7 @@ protected virtual MemberAssignment VisitMemberAssignment(MemberAssignment assign
239243

240244
protected virtual MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding binding)
241245
{
242-
IEnumerable<MemberBinding> bindings = this.VisitBindingList(binding.Bindings);
246+
IEnumerable<MemberBinding> bindings = VisitBindingList(binding.Bindings);
243247
if (bindings != binding.Bindings)
244248
{
245249
return Expression.MemberBind(binding.Member, bindings);
@@ -250,7 +254,7 @@ protected virtual MemberMemberBinding VisitMemberMemberBinding(MemberMemberBindi
250254

251255
protected virtual MemberListBinding VisitMemberListBinding(MemberListBinding binding)
252256
{
253-
IEnumerable<ElementInit> initializers = this.VisitElementInitializerList(binding.Initializers);
257+
IEnumerable<ElementInit> initializers = VisitElementInitializerList(binding.Initializers);
254258
if (initializers != binding.Initializers)
255259
{
256260
return Expression.ListBind(binding.Member, initializers);
@@ -261,10 +265,10 @@ protected virtual MemberListBinding VisitMemberListBinding(MemberListBinding bin
261265

262266
protected virtual IEnumerable<MemberBinding> VisitBindingList(ReadOnlyCollection<MemberBinding> original)
263267
{
264-
List<MemberBinding> list = null;
268+
List<MemberBinding>? list = null;
265269
for (int i = 0, n = original.Count; i < n; i++)
266270
{
267-
MemberBinding b = this.VisitBinding(original[i]);
271+
MemberBinding b = VisitBinding(original[i]);
268272
if (list != null)
269273
{
270274
list.Add(b);
@@ -288,10 +292,10 @@ protected virtual IEnumerable<MemberBinding> VisitBindingList(ReadOnlyCollection
288292

289293
protected virtual IEnumerable<ElementInit> VisitElementInitializerList(ReadOnlyCollection<ElementInit> original)
290294
{
291-
List<ElementInit> list = null;
295+
List<ElementInit>? list = null;
292296
for (int i = 0, n = original.Count; i < n; i++)
293297
{
294-
ElementInit init = this.VisitElementInitializer(original[i]);
298+
ElementInit init = VisitElementInitializer(original[i]);
295299
if (list != null)
296300
{
297301
list.Add(init);
@@ -326,7 +330,7 @@ protected virtual Expression VisitLambda(LambdaExpression lambda)
326330

327331
protected virtual NewExpression VisitNew(NewExpression nex)
328332
{
329-
IEnumerable<Expression> args = this.VisitExpressionList(nex.Arguments);
333+
IEnumerable<Expression> args = VisitExpressionList(nex.Arguments);
330334
if (args != nex.Arguments)
331335
{
332336
if (nex.Members != null)
@@ -341,7 +345,7 @@ protected virtual NewExpression VisitNew(NewExpression nex)
341345
protected virtual Expression VisitMemberInit(MemberInitExpression init)
342346
{
343347
NewExpression n = VisitNew(init.NewExpression);
344-
IEnumerable<MemberBinding> bindings = this.VisitBindingList(init.Bindings);
348+
IEnumerable<MemberBinding> bindings = VisitBindingList(init.Bindings);
345349
if (n != init.NewExpression || bindings != init.Bindings)
346350
{
347351
return Expression.MemberInit(n, bindings);
@@ -353,7 +357,7 @@ protected virtual Expression VisitMemberInit(MemberInitExpression init)
353357
protected virtual Expression VisitListInit(ListInitExpression init)
354358
{
355359
NewExpression n = VisitNew(init.NewExpression);
356-
IEnumerable<ElementInit> initializers = this.VisitElementInitializerList(init.Initializers);
360+
IEnumerable<ElementInit> initializers = VisitElementInitializerList(init.Initializers);
357361
if (n != init.NewExpression || initializers != init.Initializers)
358362
{
359363
return Expression.ListInit(n, initializers);
@@ -364,25 +368,23 @@ protected virtual Expression VisitListInit(ListInitExpression init)
364368

365369
protected virtual Expression VisitNewArray(NewArrayExpression na)
366370
{
367-
IEnumerable<Expression> exprs = this.VisitExpressionList(na.Expressions);
371+
IEnumerable<Expression> exprs = VisitExpressionList(na.Expressions);
368372
if (exprs != na.Expressions)
369373
{
370374
if (na.NodeType == ExpressionType.NewArrayInit)
371375
{
372-
return Expression.NewArrayInit(na.Type.GetElementType(), exprs);
373-
}
374-
else
375-
{
376-
return Expression.NewArrayBounds(na.Type.GetElementType(), exprs);
376+
return Expression.NewArrayInit(na.Type.GetElementType()!, exprs);
377377
}
378+
379+
return Expression.NewArrayBounds(na.Type.GetElementType()!, exprs);
378380
}
379381

380382
return na;
381383
}
382384

383385
protected virtual Expression VisitInvocation(InvocationExpression iv)
384386
{
385-
IEnumerable<Expression> args = this.VisitExpressionList(iv.Arguments);
387+
IEnumerable<Expression> args = VisitExpressionList(iv.Arguments);
386388
Expression expr = Visit(iv.Expression);
387389
if (args != iv.Arguments || expr != iv.Expression)
388390
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ReSharper disable once CheckNamespace
2+
namespace System;
3+
4+
internal static class StringExtensions
5+
{
6+
/// <summary>
7+
/// Indicates whether a specified string is null, empty, or consists only of white-space
8+
/// characters.
9+
///
10+
/// Recreates the same functionality as System.String.IsNullOrWhiteSpace but included here
11+
/// for compatibility with net35.
12+
/// </summary>
13+
/// <param name="value">The string to test.</param>
14+
/// <returns>
15+
/// true if the value parameter is null or System.String.Empty, or if value consists
16+
/// exclusively of white-space characters.
17+
/// </returns>
18+
public static bool IsNullOrWhiteSpace(this string? value)
19+
{
20+
#if !NET35
21+
return string.IsNullOrWhiteSpace(value);
22+
#else
23+
if (value == null)
24+
{
25+
return true;
26+
}
27+
28+
for (int i = 0; i < value.Length; i++)
29+
{
30+
if (!char.IsWhiteSpace(value[i]))
31+
{
32+
return false;
33+
}
34+
}
35+
36+
return true;
37+
#endif
38+
}
39+
}

src/System.Linq.Dynamic.Core/CustomTypeProviders/AbstractDynamicLinqCustomTypeProvider.cs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using JetBrains.Annotations;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq.Dynamic.Core.Validation;
43
using System.Reflection;
54

@@ -15,7 +14,7 @@ public abstract class AbstractDynamicLinqCustomTypeProvider
1514
/// </summary>
1615
/// <param name="assemblies">The assemblies to process.</param>
1716
/// <returns><see cref="IEnumerable{Type}" /></returns>
18-
protected IEnumerable<Type> FindTypesMarkedWithDynamicLinqTypeAttribute([NotNull] IEnumerable<Assembly> assemblies)
17+
protected IEnumerable<Type> FindTypesMarkedWithDynamicLinqTypeAttribute(IEnumerable<Assembly> assemblies)
1918
{
2019
Check.NotNull(assemblies, nameof(assemblies));
2120
#if !NET35
@@ -30,7 +29,7 @@ protected IEnumerable<Type> FindTypesMarkedWithDynamicLinqTypeAttribute([NotNull
3029
/// <param name="assemblies">The assemblies to inspect.</param>
3130
/// <param name="typeName">The typename to resolve.</param>
3231
/// <returns>A resolved <see cref="Type"/> or null when not found.</returns>
33-
protected Type ResolveType([NotNull] IEnumerable<Assembly> assemblies, [NotNull] string typeName)
32+
protected Type? ResolveType(IEnumerable<Assembly> assemblies, string typeName)
3433
{
3534
Check.NotNull(assemblies, nameof(assemblies));
3635
Check.NotEmpty(typeName, nameof(typeName));
@@ -53,19 +52,19 @@ protected Type ResolveType([NotNull] IEnumerable<Assembly> assemblies, [NotNull]
5352
/// <param name="assemblies">The assemblies to inspect.</param>
5453
/// <param name="simpleTypeName">The simple typename to resolve.</param>
5554
/// <returns>A resolved <see cref="Type"/> or null when not found.</returns>
56-
protected Type ResolveTypeBySimpleName([NotNull] IEnumerable<Assembly> assemblies, [NotNull] string simpleTypeName)
55+
protected Type? ResolveTypeBySimpleName(IEnumerable<Assembly> assemblies, string simpleTypeName)
5756
{
5857
Check.NotNull(assemblies, nameof(assemblies));
5958
Check.NotEmpty(simpleTypeName, nameof(simpleTypeName));
6059

6160
foreach (var assembly in assemblies)
6261
{
63-
var fullNames = assembly.GetTypes().Select(t => t.FullName).Distinct();
62+
var fullNames = assembly.GetTypes().Select(t => t.FullName!).Distinct();
6463
var firstMatchingFullname = fullNames.FirstOrDefault(fn => fn.EndsWith($".{simpleTypeName}"));
6564

6665
if (firstMatchingFullname != null)
6766
{
68-
Type resolvedType = assembly.GetType(firstMatchingFullname, false, true);
67+
var resolvedType = assembly.GetType(firstMatchingFullname, false, true);
6968
if (resolvedType != null)
7069
{
7170
return resolvedType;
@@ -82,13 +81,13 @@ protected Type ResolveTypeBySimpleName([NotNull] IEnumerable<Assembly> assemblie
8281
/// </summary>
8382
/// <param name="assemblies">The assemblies to process.</param>
8483
/// <returns><see cref="IEnumerable{Type}" /></returns>
85-
protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute([NotNull] IEnumerable<Assembly> assemblies)
84+
protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute(IEnumerable<Assembly> assemblies)
8685
{
8786
Check.NotNull(assemblies, nameof(assemblies));
8887

8988
foreach (var assembly in assemblies)
9089
{
91-
Type[] definedTypes = null;
90+
Type[]? definedTypes = null;
9291

9392
try
9493
{
@@ -118,13 +117,13 @@ protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute([NotNul
118117
/// </summary>
119118
/// <param name="assemblies">The assemblies to process.</param>
120119
/// <returns><see cref="IEnumerable{Type}" /></returns>
121-
protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute([NotNull] IEnumerable<Assembly> assemblies)
120+
protected IEnumerable<Type> GetAssemblyTypesWithDynamicLinqTypeAttribute(IEnumerable<Assembly> assemblies)
122121
{
123122
Check.NotNull(assemblies, nameof(assemblies));
124123

125124
foreach (var assembly in assemblies.Where(a => !a.GlobalAssemblyCache)) // Skip System DLL's
126125
{
127-
Type[] definedTypes = null;
126+
Type[]? definedTypes = null;
128127

129128
try
130129
{

src/System.Linq.Dynamic.Core/CustomTypeProviders/DefaultDynamicLinqCustomTypeProvider.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace System.Linq.Dynamic.Core.CustomTypeProviders
77
{
88
/// <summary>
9-
/// The default implementation for <see cref="IDynamicLinkCustomTypeProvider"/>.
9+
/// The default implementation for <see cref="DefaultDynamicLinqCustomTypeProvider"/>.
1010
///
1111
/// Scans the current AppDomain for all types marked with <see cref="DynamicLinqTypeAttribute"/>, and adds them as custom Dynamic Link types.
1212
///
@@ -19,8 +19,8 @@ public class DefaultDynamicLinqCustomTypeProvider : AbstractDynamicLinqCustomTyp
1919
private readonly IAssemblyHelper _assemblyHelper = new DefaultAssemblyHelper();
2020
private readonly bool _cacheCustomTypes;
2121

22-
private HashSet<Type> _cachedCustomTypes;
23-
private Dictionary<Type, List<MethodInfo>> _cachedExtensionMethods;
22+
private HashSet<Type>? _cachedCustomTypes;
23+
private Dictionary<Type, List<MethodInfo>>? _cachedExtensionMethods;
2424

2525
/// <summary>
2626
/// Initializes a new instance of the <see cref="DefaultDynamicLinqCustomTypeProvider"/> class.
@@ -64,7 +64,7 @@ public Dictionary<Type, List<MethodInfo>> GetExtensionMethods()
6464
}
6565

6666
/// <inheritdoc cref="IDynamicLinqCustomTypeProvider.ResolveType"/>
67-
public Type ResolveType(string typeName)
67+
public Type? ResolveType(string typeName)
6868
{
6969
Check.NotEmpty(typeName, nameof(typeName));
7070

@@ -73,7 +73,7 @@ public Type ResolveType(string typeName)
7373
}
7474

7575
/// <inheritdoc cref="IDynamicLinqCustomTypeProvider.ResolveTypeBySimpleName"/>
76-
public Type ResolveTypeBySimpleName(string simpleTypeName)
76+
public Type? ResolveTypeBySimpleName(string simpleTypeName)
7777
{
7878
Check.NotEmpty(simpleTypeName, nameof(simpleTypeName));
7979

@@ -106,4 +106,4 @@ private Dictionary<Type, List<MethodInfo>> GetExtensionMethodsInternal()
106106
return list.GroupBy(x => x.Item1, tuple => tuple.Item2).ToDictionary(key => key.Key, methods => methods.ToList());
107107
}
108108
}
109-
}
109+
}

src/System.Linq.Dynamic.Core/CustomTypeProviders/IDynamicLinqCustomTypeProvider.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public interface IDynamicLinqCustomTypeProvider
2626
/// </summary>
2727
/// <param name="typeName">The typename to resolve.</param>
2828
/// <returns>A resolved <see cref="Type"/> or null when not found.</returns>
29-
Type ResolveType([NotNull] string typeName);
29+
Type? ResolveType(string typeName);
3030

3131
/// <summary>
3232
/// Resolve any type by the simple name which is registered in the current application domain.
3333
/// </summary>
3434
/// <param name="simpleTypeName">The typename to resolve.</param>
3535
/// <returns>A resolved <see cref="Type"/> or null when not found.</returns>
36-
Type ResolveTypeBySimpleName([NotNull] string simpleTypeName);
36+
Type? ResolveTypeBySimpleName(string simpleTypeName);
3737
}
38-
}
38+
}

0 commit comments

Comments
 (0)