diff --git a/System.Linq.Dynamic.Core.sln b/System.Linq.Dynamic.Core.sln index 1cabb9fa..3e56b2bb 100644 --- a/System.Linq.Dynamic.Core.sln +++ b/System.Linq.Dynamic.Core.sln @@ -145,6 +145,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Linq.Dynamic.Core.Te EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WasmDynamicLinq", "src-blazor\WasmDynamicLinq\WasmDynamicLinq.csproj", "{2DE2052F-0A50-40C7-B6FF-52B52386BF9A}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SldcTrimmer", "src-examples\SldcTrimmer\SldcTrimmer.csproj", "{7A31366C-DD98-41A3-A0C1-A97068BD9658}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -913,6 +915,22 @@ Global {2DE2052F-0A50-40C7-B6FF-52B52386BF9A}.Release|x64.Build.0 = Release|Any CPU {2DE2052F-0A50-40C7-B6FF-52B52386BF9A}.Release|x86.ActiveCfg = Release|Any CPU {2DE2052F-0A50-40C7-B6FF-52B52386BF9A}.Release|x86.Build.0 = Release|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Debug|ARM.ActiveCfg = Debug|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Debug|ARM.Build.0 = Debug|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Debug|x64.ActiveCfg = Debug|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Debug|x64.Build.0 = Debug|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Debug|x86.ActiveCfg = Debug|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Debug|x86.Build.0 = Debug|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Release|Any CPU.Build.0 = Release|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Release|ARM.ActiveCfg = Release|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Release|ARM.Build.0 = Release|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Release|x64.ActiveCfg = Release|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Release|x64.Build.0 = Release|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Release|x86.ActiveCfg = Release|Any CPU + {7A31366C-DD98-41A3-A0C1-A97068BD9658}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -965,6 +983,7 @@ Global {B01B327C-FC68-49B6-BDE3-A13D0C66DF5C} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62} {7AFC2836-0F6E-4B0D-8BB3-13317A3B6616} = {8463ED7E-69FB-49AE-85CF-0791AFD98E38} {2DE2052F-0A50-40C7-B6FF-52B52386BF9A} = {122BC4FA-7563-4E35-9D17-077F16F1629F} + {7A31366C-DD98-41A3-A0C1-A97068BD9658} = {BCA2A024-9032-4E56-A6C4-17A15D921728} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {94C56722-194E-4B8B-BC23-B3F754E89A20} diff --git a/System.Linq.Dynamic.Core.sln.DotSettings b/System.Linq.Dynamic.Core.sln.DotSettings index d5856118..766d5f30 100644 --- a/System.Linq.Dynamic.Core.sln.DotSettings +++ b/System.Linq.Dynamic.Core.sln.DotSettings @@ -3,6 +3,7 @@ IL UTC WASM + True True True True diff --git a/src-examples/SldcTrimmer/Color.cs b/src-examples/SldcTrimmer/Color.cs new file mode 100644 index 00000000..19539daf --- /dev/null +++ b/src-examples/SldcTrimmer/Color.cs @@ -0,0 +1,13 @@ +namespace SldcTrimmer +{ + internal class Color + { + public byte Alpha { get; set; } + public byte Red { get; set; } + public byte Green { get; set; } + public byte Blue { get; set; } + public float Hue { get; set; } + public float Saturation { get; set; } + public float Brightness { get; set; } + } +} diff --git a/src-examples/SldcTrimmer/LambdaExtensions.cs b/src-examples/SldcTrimmer/LambdaExtensions.cs new file mode 100644 index 00000000..94e2f31f --- /dev/null +++ b/src-examples/SldcTrimmer/LambdaExtensions.cs @@ -0,0 +1,13 @@ +using System; +using System.Linq.Dynamic.Core; + +namespace SldcTrimmer +{ + internal static class LambdaExtensions + { + public static Func ParseLambda(this string expression) + { + return DynamicExpressionParser.ParseLambda(null, false, expression).Compile(); + } + } +} diff --git a/src-examples/SldcTrimmer/Program.cs b/src-examples/SldcTrimmer/Program.cs new file mode 100644 index 00000000..22870d18 --- /dev/null +++ b/src-examples/SldcTrimmer/Program.cs @@ -0,0 +1,32 @@ +using System; + +namespace SldcTrimmer +{ + class Program + { + static void Main(string[] args) + { + try + { + const string filterStr = "Alpha == 255"; + Console.WriteLine(filterStr); + + var filter = filterStr.ParseLambda(); + + var color1 = new Color { Alpha = 255 }; + var value1 = filter(color1); + Console.WriteLine($"{color1.Alpha} -> {value1}"); + + var color2 = new Color { Alpha = 128 }; + var value2 = filter(color2); + Console.WriteLine($"{color2.Alpha} -> {value2}"); + } + catch (Exception e) + { + Console.WriteLine(e.GetType()); + Console.WriteLine(e.Message); + Console.WriteLine(e.StackTrace); + } + } + } +} \ No newline at end of file diff --git a/src-examples/SldcTrimmer/Properties/PublishProfiles/FolderProfile.pubxml b/src-examples/SldcTrimmer/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 00000000..56392783 --- /dev/null +++ b/src-examples/SldcTrimmer/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,18 @@ + + + + + Release + Any CPU + C:\temp\x + FileSystem + <_TargetId>Folder + net8.0 + win-x64 + true + true + true + + \ No newline at end of file diff --git a/src-examples/SldcTrimmer/SldcTrimmer.csproj b/src-examples/SldcTrimmer/SldcTrimmer.csproj new file mode 100644 index 00000000..f50182f0 --- /dev/null +++ b/src-examples/SldcTrimmer/SldcTrimmer.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + + + + true + full + + + + + + + + + + + + diff --git a/src/System.Linq.Dynamic.Core/AnyOfTypes/AnyOfTypes.g.cs b/src/System.Linq.Dynamic.Core/AnyOfTypes/AnyOfTypes.g.cs index 840b4ddd..62400494 100644 --- a/src/System.Linq.Dynamic.Core/AnyOfTypes/AnyOfTypes.g.cs +++ b/src/System.Linq.Dynamic.Core/AnyOfTypes/AnyOfTypes.g.cs @@ -11,6 +11,6 @@ namespace AnyOfTypes { internal enum AnyOfType { - Undefined = 0, First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth, Ninth, Tenth + Undefined = 0, First, Second } } \ No newline at end of file diff --git a/src/System.Linq.Dynamic.Core/Compatibility/CodeAnalysis/DynamicallyAccessedMemberTypes.cs b/src/System.Linq.Dynamic.Core/Compatibility/CodeAnalysis/DynamicallyAccessedMemberTypes.cs new file mode 100644 index 00000000..5b689575 --- /dev/null +++ b/src/System.Linq.Dynamic.Core/Compatibility/CodeAnalysis/DynamicallyAccessedMemberTypes.cs @@ -0,0 +1,79 @@ +#if !NET6_0_OR_GREATER + +// ReSharper disable once CheckNamespace +namespace System.Diagnostics.CodeAnalysis; + +// +// Summary: +// Specifies the types of members that are dynamically accessed. This enumeration +// has a System.FlagsAttribute attribute that allows a bitwise combination of its +// member values. +[Flags] +internal enum DynamicallyAccessedMemberTypes +{ + // + // Summary: + // Specifies all members. + All = -1, + // + // Summary: + // Specifies no members. + None = 0, + // + // Summary: + // Specifies the default, parameterless public constructor. + PublicParameterlessConstructor = 1, + // + // Summary: + // Specifies all public constructors. + PublicConstructors = 3, + // + // Summary: + // Specifies all non-public constructors. + NonPublicConstructors = 4, + // + // Summary: + // Specifies all public methods. + PublicMethods = 8, + // + // Summary: + // Specifies all non-public methods. + NonPublicMethods = 16, + // + // Summary: + // Specifies all public fields. + PublicFields = 32, + // + // Summary: + // Specifies all non-public fields. + NonPublicFields = 64, + // + // Summary: + // Specifies all public nested types. + PublicNestedTypes = 128, + // + // Summary: + // Specifies all non-public nested types. + NonPublicNestedTypes = 256, + // + // Summary: + // Specifies all public properties. + PublicProperties = 512, + // + // Summary: + // Specifies all non-public properties. + NonPublicProperties = 1024, + // + // Summary: + // Specifies all public events. + PublicEvents = 2048, + // + // Summary: + // Specifies all non-public events. + NonPublicEvents = 4096, + // + // Summary: + // Specifies all interfaces implemented by the type. + Interfaces = 8192 +} +#endif \ No newline at end of file diff --git a/src/System.Linq.Dynamic.Core/Compatibility/CodeAnalysis/DynamicallyAccessedMembersAttribute.cs b/src/System.Linq.Dynamic.Core/Compatibility/CodeAnalysis/DynamicallyAccessedMembersAttribute.cs new file mode 100644 index 00000000..77d9f97a --- /dev/null +++ b/src/System.Linq.Dynamic.Core/Compatibility/CodeAnalysis/DynamicallyAccessedMembersAttribute.cs @@ -0,0 +1,16 @@ +#if !NET6_0_OR_GREATER + +// ReSharper disable once CheckNamespace +namespace System.Diagnostics.CodeAnalysis; + +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, Inherited = false)] +internal sealed class DynamicallyAccessedMembersAttribute : Attribute +{ + public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes memberTypes) + { + MemberTypes = memberTypes; + } + + public DynamicallyAccessedMemberTypes MemberTypes { get; } +} +#endif \ No newline at end of file diff --git a/src/System.Linq.Dynamic.Core/Compatibility/Nullable/NotNullWhenAttribute.cs b/src/System.Linq.Dynamic.Core/Compatibility/CodeAnalysis/NotNullWhenAttribute.cs similarity index 100% rename from src/System.Linq.Dynamic.Core/Compatibility/Nullable/NotNullWhenAttribute.cs rename to src/System.Linq.Dynamic.Core/Compatibility/CodeAnalysis/NotNullWhenAttribute.cs diff --git a/src/System.Linq.Dynamic.Core/Compatibility/ExpressionVisitor.cs b/src/System.Linq.Dynamic.Core/Compatibility/ExpressionVisitor.cs index 3f1cd563..bf063e1f 100644 --- a/src/System.Linq.Dynamic.Core/Compatibility/ExpressionVisitor.cs +++ b/src/System.Linq.Dynamic.Core/Compatibility/ExpressionVisitor.cs @@ -1,398 +1,389 @@ #if NET35 using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Linq.Dynamic.Core.Validation; -namespace System.Linq.Expressions +// ReSharper disable once CheckNamespace +namespace System.Linq.Expressions; + +/// +/// Code copied from https://blogs.msdn.microsoft.com/mattwar/2007/07/31/linq-building-an-iqueryable-provider-part-ii/ +/// +internal abstract class ExpressionVisitor { - /// - /// Code copied from https://blogs.msdn.microsoft.com/mattwar/2007/07/31/linq-building-an-iqueryable-provider-part-ii/ - /// - internal abstract class ExpressionVisitor + protected virtual Expression Visit(Expression exp) { - /// - /// Initializes a new instance of the class. - /// - protected ExpressionVisitor() + Check.NotNull(exp); + + switch (exp.NodeType) { + case ExpressionType.Negate: + case ExpressionType.NegateChecked: + case ExpressionType.Not: + case ExpressionType.Convert: + case ExpressionType.ConvertChecked: + case ExpressionType.ArrayLength: + case ExpressionType.Quote: + case ExpressionType.TypeAs: + return VisitUnary((UnaryExpression)exp); + case ExpressionType.Add: + case ExpressionType.AddChecked: + case ExpressionType.Subtract: + case ExpressionType.SubtractChecked: + case ExpressionType.Multiply: + case ExpressionType.MultiplyChecked: + case ExpressionType.Divide: + case ExpressionType.Modulo: + case ExpressionType.And: + case ExpressionType.AndAlso: + case ExpressionType.Or: + case ExpressionType.OrElse: + case ExpressionType.LessThan: + case ExpressionType.LessThanOrEqual: + case ExpressionType.GreaterThan: + case ExpressionType.GreaterThanOrEqual: + case ExpressionType.Equal: + case ExpressionType.NotEqual: + case ExpressionType.Coalesce: + case ExpressionType.ArrayIndex: + case ExpressionType.RightShift: + case ExpressionType.LeftShift: + case ExpressionType.ExclusiveOr: + return VisitBinary((BinaryExpression)exp); + case ExpressionType.TypeIs: + return VisitTypeIs((TypeBinaryExpression)exp); + case ExpressionType.Conditional: + return VisitConditional((ConditionalExpression)exp); + case ExpressionType.Constant: + return VisitConstant((ConstantExpression)exp); + case ExpressionType.Parameter: + return VisitParameter((ParameterExpression)exp); + case ExpressionType.MemberAccess: + return VisitMemberAccess((MemberExpression)exp); + case ExpressionType.Call: + return VisitMethodCall((MethodCallExpression)exp); + case ExpressionType.Lambda: + return VisitLambda((LambdaExpression)exp); + case ExpressionType.New: + return VisitNew((NewExpression)exp); + case ExpressionType.NewArrayInit: + case ExpressionType.NewArrayBounds: + return VisitNewArray((NewArrayExpression)exp); + case ExpressionType.Invoke: + return VisitInvocation((InvocationExpression)exp); + case ExpressionType.MemberInit: + return VisitMemberInit((MemberInitExpression)exp); + case ExpressionType.ListInit: + return VisitListInit((ListInitExpression)exp); + default: + throw new Exception($"Unhandled expression type: '{exp.NodeType}'"); } + } - protected virtual Expression Visit(Expression exp) + protected virtual MemberBinding VisitBinding(MemberBinding binding) + { + switch (binding.BindingType) { - if (exp == null) - { - return exp; - } - - switch (exp.NodeType) - { - case ExpressionType.Negate: - case ExpressionType.NegateChecked: - case ExpressionType.Not: - case ExpressionType.Convert: - case ExpressionType.ConvertChecked: - case ExpressionType.ArrayLength: - case ExpressionType.Quote: - case ExpressionType.TypeAs: - return VisitUnary((UnaryExpression)exp); - case ExpressionType.Add: - case ExpressionType.AddChecked: - case ExpressionType.Subtract: - case ExpressionType.SubtractChecked: - case ExpressionType.Multiply: - case ExpressionType.MultiplyChecked: - case ExpressionType.Divide: - case ExpressionType.Modulo: - case ExpressionType.And: - case ExpressionType.AndAlso: - case ExpressionType.Or: - case ExpressionType.OrElse: - case ExpressionType.LessThan: - case ExpressionType.LessThanOrEqual: - case ExpressionType.GreaterThan: - case ExpressionType.GreaterThanOrEqual: - case ExpressionType.Equal: - case ExpressionType.NotEqual: - case ExpressionType.Coalesce: - case ExpressionType.ArrayIndex: - case ExpressionType.RightShift: - case ExpressionType.LeftShift: - case ExpressionType.ExclusiveOr: - return VisitBinary((BinaryExpression)exp); - case ExpressionType.TypeIs: - return VisitTypeIs((TypeBinaryExpression)exp); - case ExpressionType.Conditional: - return VisitConditional((ConditionalExpression)exp); - case ExpressionType.Constant: - return VisitConstant((ConstantExpression)exp); - case ExpressionType.Parameter: - return VisitParameter((ParameterExpression)exp); - case ExpressionType.MemberAccess: - return VisitMemberAccess((MemberExpression)exp); - case ExpressionType.Call: - return VisitMethodCall((MethodCallExpression)exp); - case ExpressionType.Lambda: - return VisitLambda((LambdaExpression)exp); - case ExpressionType.New: - return VisitNew((NewExpression)exp); - case ExpressionType.NewArrayInit: - case ExpressionType.NewArrayBounds: - return VisitNewArray((NewArrayExpression)exp); - case ExpressionType.Invoke: - return VisitInvocation((InvocationExpression)exp); - case ExpressionType.MemberInit: - return VisitMemberInit((MemberInitExpression)exp); - case ExpressionType.ListInit: - return VisitListInit((ListInitExpression)exp); - default: - throw new Exception(string.Format("Unhandled expression type: '{0}'", exp.NodeType)); - } + case MemberBindingType.Assignment: + return VisitMemberAssignment((MemberAssignment)binding); + case MemberBindingType.MemberBinding: + return VisitMemberMemberBinding((MemberMemberBinding)binding); + case MemberBindingType.ListBinding: + return VisitMemberListBinding((MemberListBinding)binding); + default: + throw new Exception($"Unhandled binding type '{binding.BindingType}'"); } + } - protected virtual MemberBinding VisitBinding(MemberBinding binding) + protected virtual ElementInit VisitElementInitializer(ElementInit initializer) + { + var arguments = VisitExpressionList(initializer.Arguments); + if (arguments != initializer.Arguments) { - switch (binding.BindingType) - { - case MemberBindingType.Assignment: - return VisitMemberAssignment((MemberAssignment)binding); - case MemberBindingType.MemberBinding: - return VisitMemberMemberBinding((MemberMemberBinding)binding); - case MemberBindingType.ListBinding: - return VisitMemberListBinding((MemberListBinding)binding); - default: - throw new Exception(string.Format("Unhandled binding type '{0}'", binding.BindingType)); - } + return Expression.ElementInit(initializer.AddMethod, arguments); } - protected virtual ElementInit VisitElementInitializer(ElementInit initializer) - { - ReadOnlyCollection arguments = VisitExpressionList(initializer.Arguments); - if (arguments != initializer.Arguments) - { - return Expression.ElementInit(initializer.AddMethod, arguments); - } + return initializer; + } - return initializer; + protected virtual Expression VisitUnary(UnaryExpression u) + { + Expression operand = Visit(u.Operand); + if (operand != u.Operand) + { + return Expression.MakeUnary(u.NodeType, operand, u.Type, u.Method); } - protected virtual Expression VisitUnary(UnaryExpression u) + return u; + } + + protected virtual Expression VisitBinary(BinaryExpression b) + { + Expression left = Visit(b.Left); + Expression right = Visit(b.Right); + Expression conversion = Visit(b.Conversion!); + if (left != b.Left || right != b.Right || conversion != b.Conversion) { - Expression operand = Visit(u.Operand); - if (operand != u.Operand) + if (b.NodeType == ExpressionType.Coalesce && b.Conversion != null) { - return Expression.MakeUnary(u.NodeType, operand, u.Type, u.Method); + return Expression.Coalesce(left, right, conversion as LambdaExpression); } - return u; + return Expression.MakeBinary(b.NodeType, left, right, b.IsLiftedToNull, b.Method); } - protected virtual Expression VisitBinary(BinaryExpression b) - { - Expression left = Visit(b.Left); - Expression right = Visit(b.Right); - Expression conversion = Visit(b.Conversion!); - if (left != b.Left || right != b.Right || conversion != b.Conversion) - { - if (b.NodeType == ExpressionType.Coalesce && b.Conversion != null) - { - return Expression.Coalesce(left, right, conversion as LambdaExpression); - } - - return Expression.MakeBinary(b.NodeType, left, right, b.IsLiftedToNull, b.Method); - } + return b; + } - return b; + protected virtual Expression VisitTypeIs(TypeBinaryExpression b) + { + Expression expr = Visit(b.Expression); + if (expr != b.Expression) + { + return Expression.TypeIs(expr, b.TypeOperand); } - protected virtual Expression VisitTypeIs(TypeBinaryExpression b) - { - Expression expr = Visit(b.Expression); - if (expr != b.Expression) - { - return Expression.TypeIs(expr, b.TypeOperand); - } + return b; + } - return b; - } + protected virtual Expression VisitConstant(ConstantExpression c) + { + return c; + } - protected virtual Expression VisitConstant(ConstantExpression c) + protected virtual Expression VisitConditional(ConditionalExpression c) + { + Expression test = Visit(c.Test); + Expression ifTrue = Visit(c.IfTrue); + Expression ifFalse = Visit(c.IfFalse); + if (test != c.Test || ifTrue != c.IfTrue || ifFalse != c.IfFalse) { - return c; + return Expression.Condition(test, ifTrue, ifFalse); } - protected virtual Expression VisitConditional(ConditionalExpression c) - { - Expression test = Visit(c.Test); - Expression ifTrue = Visit(c.IfTrue); - Expression ifFalse = Visit(c.IfFalse); - if (test != c.Test || ifTrue != c.IfTrue || ifFalse != c.IfFalse) - { - return Expression.Condition(test, ifTrue, ifFalse); - } + return c; + } - return c; - } + protected virtual Expression VisitParameter(ParameterExpression p) + { + return p; + } - protected virtual Expression VisitParameter(ParameterExpression p) + protected virtual Expression VisitMemberAccess(MemberExpression m) + { + Expression exp = Visit(m.Expression); + if (exp != m.Expression) { - return p; + return Expression.MakeMemberAccess(exp, m.Member); } - protected virtual Expression VisitMemberAccess(MemberExpression m) - { - Expression exp = Visit(m.Expression); - if (exp != m.Expression) - { - return Expression.MakeMemberAccess(exp, m.Member); - } + return m; + } - return m; + protected virtual Expression VisitMethodCall(MethodCallExpression m) + { + Expression obj = Visit(m.Object!); + IEnumerable args = VisitExpressionList(m.Arguments); + if (obj != m.Object || args != m.Arguments) + { + return Expression.Call(obj, m.Method, args); } - protected virtual Expression VisitMethodCall(MethodCallExpression m) + return m; + } + + protected virtual ReadOnlyCollection VisitExpressionList(ReadOnlyCollection original) + { + List? list = null; + for (int i = 0, n = original.Count; i < n; i++) { - Expression obj = Visit(m.Object!); - IEnumerable args = VisitExpressionList(m.Arguments); - if (obj != m.Object || args != m.Arguments) + Expression p = Visit(original[i]); + if (list != null) { - return Expression.Call(obj, m.Method, args); + list.Add(p); } - - return m; - } - - protected virtual ReadOnlyCollection VisitExpressionList(ReadOnlyCollection original) - { - List? list = null; - for (int i = 0, n = original.Count; i < n; i++) + else if (p != original[i]) { - Expression p = Visit(original[i]); - if (list != null) - { - list.Add(p); - } - else if (p != original[i]) + list = new List(n); + for (int j = 0; j < i; j++) { - list = new List(n); - for (int j = 0; j < i; j++) - { - list.Add(original[j]); - } - - list.Add(p); + list.Add(original[j]); } - } - if (list != null) - { - return list.AsReadOnly(); + list.Add(p); } - - return original; } - protected virtual MemberAssignment VisitMemberAssignment(MemberAssignment assignment) + if (list != null) { - Expression e = Visit(assignment.Expression); - if (e != assignment.Expression) - { - return Expression.Bind(assignment.Member, e); - } - - return assignment; + return list.AsReadOnly(); } - protected virtual MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding binding) - { - IEnumerable bindings = VisitBindingList(binding.Bindings); - if (bindings != binding.Bindings) - { - return Expression.MemberBind(binding.Member, bindings); - } + return original; + } - return binding; + protected virtual MemberAssignment VisitMemberAssignment(MemberAssignment assignment) + { + Expression e = Visit(assignment.Expression); + if (e != assignment.Expression) + { + return Expression.Bind(assignment.Member, e); } - protected virtual MemberListBinding VisitMemberListBinding(MemberListBinding binding) + return assignment; + } + + protected virtual MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding binding) + { + IEnumerable bindings = VisitBindingList(binding.Bindings); + if (bindings != binding.Bindings) { - IEnumerable initializers = VisitElementInitializerList(binding.Initializers); - if (initializers != binding.Initializers) - { - return Expression.ListBind(binding.Member, initializers); - } + return Expression.MemberBind(binding.Member, bindings); + } - return binding; + return binding; + } + + protected virtual MemberListBinding VisitMemberListBinding(MemberListBinding binding) + { + IEnumerable initializers = VisitElementInitializerList(binding.Initializers); + if (initializers != binding.Initializers) + { + return Expression.ListBind(binding.Member, initializers); } - protected virtual IEnumerable VisitBindingList(ReadOnlyCollection original) + return binding; + } + + protected virtual IEnumerable VisitBindingList(ReadOnlyCollection original) + { + List? list = null; + for (int i = 0, n = original.Count; i < n; i++) { - List? list = null; - for (int i = 0, n = original.Count; i < n; i++) + MemberBinding b = VisitBinding(original[i]); + if (list != null) + { + list.Add(b); + } + else if (b != original[i]) { - MemberBinding b = VisitBinding(original[i]); - if (list != null) + list = new List(n); + for (int j = 0; j < i; j++) { - list.Add(b); + list.Add(original[j]); } - else if (b != original[i]) - { - list = new List(n); - for (int j = 0; j < i; j++) - { - list.Add(original[j]); - } - list.Add(b); - } + list.Add(b); } - - if (list != null) - return list; - return original; } - protected virtual IEnumerable VisitElementInitializerList(ReadOnlyCollection original) + if (list != null) + return list; + return original; + } + + protected virtual IEnumerable VisitElementInitializerList(ReadOnlyCollection original) + { + List? list = null; + for (int i = 0, n = original.Count; i < n; i++) { - List? list = null; - for (int i = 0, n = original.Count; i < n; i++) + ElementInit init = VisitElementInitializer(original[i]); + if (list != null) { - ElementInit init = VisitElementInitializer(original[i]); - if (list != null) + list.Add(init); + } + else if (init != original[i]) + { + list = new List(n); + for (int j = 0; j < i; j++) { - list.Add(init); + list.Add(original[j]); } - else if (init != original[i]) - { - list = new List(n); - for (int j = 0; j < i; j++) - { - list.Add(original[j]); - } - list.Add(init); - } + list.Add(init); } - - if (list != null) - return list; - return original; } - protected virtual Expression VisitLambda(LambdaExpression lambda) - { - Expression body = Visit(lambda.Body); - if (body != lambda.Body) - { - return Expression.Lambda(lambda.Type, body, lambda.Parameters); - } + if (list != null) + return list; + return original; + } - return lambda; + protected virtual Expression VisitLambda(LambdaExpression lambda) + { + Expression body = Visit(lambda.Body); + if (body != lambda.Body) + { + return Expression.Lambda(lambda.Type, body, lambda.Parameters); } - protected virtual NewExpression VisitNew(NewExpression nex) - { - IEnumerable args = VisitExpressionList(nex.Arguments); - if (args != nex.Arguments) - { - if (nex.Members != null) - return Expression.New(nex.Constructor, args, nex.Members); - else - return Expression.New(nex.Constructor, args); - } + return lambda; + } - return nex; + protected virtual NewExpression VisitNew(NewExpression nex) + { + IEnumerable args = VisitExpressionList(nex.Arguments); + if (args != nex.Arguments) + { + if (nex.Members != null) + return Expression.New(nex.Constructor, args, nex.Members); + else + return Expression.New(nex.Constructor, args); } - protected virtual Expression VisitMemberInit(MemberInitExpression init) - { - NewExpression n = VisitNew(init.NewExpression); - IEnumerable bindings = VisitBindingList(init.Bindings); - if (n != init.NewExpression || bindings != init.Bindings) - { - return Expression.MemberInit(n, bindings); - } + return nex; + } - return init; + protected virtual Expression VisitMemberInit(MemberInitExpression init) + { + NewExpression n = VisitNew(init.NewExpression); + IEnumerable bindings = VisitBindingList(init.Bindings); + if (n != init.NewExpression || bindings != init.Bindings) + { + return Expression.MemberInit(n, bindings); } - protected virtual Expression VisitListInit(ListInitExpression init) - { - NewExpression n = VisitNew(init.NewExpression); - IEnumerable initializers = VisitElementInitializerList(init.Initializers); - if (n != init.NewExpression || initializers != init.Initializers) - { - return Expression.ListInit(n, initializers); - } + return init; + } - return init; + protected virtual Expression VisitListInit(ListInitExpression init) + { + NewExpression n = VisitNew(init.NewExpression); + IEnumerable initializers = VisitElementInitializerList(init.Initializers); + if (n != init.NewExpression || initializers != init.Initializers) + { + return Expression.ListInit(n, initializers); } - protected virtual Expression VisitNewArray(NewArrayExpression na) + return init; + } + + protected virtual Expression VisitNewArray(NewArrayExpression na) + { + IEnumerable exprs = VisitExpressionList(na.Expressions); + if (exprs != na.Expressions) { - IEnumerable exprs = VisitExpressionList(na.Expressions); - if (exprs != na.Expressions) + if (na.NodeType == ExpressionType.NewArrayInit) { - if (na.NodeType == ExpressionType.NewArrayInit) - { - return Expression.NewArrayInit(na.Type.GetElementType()!, exprs); - } - - return Expression.NewArrayBounds(na.Type.GetElementType()!, exprs); + return Expression.NewArrayInit(na.Type.GetElementType()!, exprs); } - return na; + return Expression.NewArrayBounds(na.Type.GetElementType()!, exprs); } - protected virtual Expression VisitInvocation(InvocationExpression iv) - { - IEnumerable args = VisitExpressionList(iv.Arguments); - Expression expr = Visit(iv.Expression); - if (args != iv.Arguments || expr != iv.Expression) - { - return Expression.Invoke(expr, args); - } + return na; + } - return iv; + protected virtual Expression VisitInvocation(InvocationExpression iv) + { + IEnumerable args = VisitExpressionList(iv.Arguments); + Expression expr = Visit(iv.Expression); + if (args != iv.Arguments || expr != iv.Expression) + { + return Expression.Invoke(expr, args); } + + return iv; } } -#endif +#endif \ No newline at end of file diff --git a/src/System.Linq.Dynamic.Core/DynamicEnumerableExtensions.cs b/src/System.Linq.Dynamic.Core/DynamicEnumerableExtensions.cs index b669fafe..3ecde061 100644 --- a/src/System.Linq.Dynamic.Core/DynamicEnumerableExtensions.cs +++ b/src/System.Linq.Dynamic.Core/DynamicEnumerableExtensions.cs @@ -61,7 +61,7 @@ public static dynamic[] ToDynamicArray(this IEnumerable source, Type type) Check.NotNull(source); Check.NotNull(type); - IEnumerable result = (IEnumerable)ToDynamicArrayGenericMethod.MakeGenericMethod(type).Invoke(source, new object[] { source }); + var result = (IEnumerable)ToDynamicArrayGenericMethod.MakeGenericMethod(type).Invoke(source, [source])!; #if NET35 return CastToArray(result); #else diff --git a/src/System.Linq.Dynamic.Core/DynamicOrdering.cs b/src/System.Linq.Dynamic.Core/DynamicOrdering.cs index e6b0ffbb..98e33167 100644 --- a/src/System.Linq.Dynamic.Core/DynamicOrdering.cs +++ b/src/System.Linq.Dynamic.Core/DynamicOrdering.cs @@ -1,11 +1,17 @@ using System.Linq.Expressions; -namespace System.Linq.Dynamic.Core +namespace System.Linq.Dynamic.Core; + +internal struct DynamicOrdering { - internal class DynamicOrdering + public Expression Selector { get; } + public bool Ascending { get; } + public string MethodName { get; } + + public DynamicOrdering(Expression selector, bool ascending, string methodName) { - public Expression Selector; - public bool Ascending; - public string MethodName; + Selector = selector; + Ascending = ascending; + MethodName = methodName; } } \ No newline at end of file diff --git a/src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs b/src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs index 3c4d21bb..e2f7abd5 100644 --- a/src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs +++ b/src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs @@ -1938,14 +1938,14 @@ private static IQueryable SelectManyInternal(IQueryable source, ParsingConfig co } //we have to adjust to lambda to return an IEnumerable instead of whatever the actual property is. - Type enumerableType = typeof(IEnumerable<>).MakeGenericType(resultType); + Type enumerableType = typeof(IEnumerable<>).MakeGenericType(resultType!); Type inputType = source.Expression.Type.GetTypeInfo().GetGenericTypeArguments()[0]; Type delegateType = typeof(Func<,>).MakeGenericType(inputType, enumerableType); lambda = Expression.Lambda(delegateType, lambda.Body, lambda.Parameters); var optimized = OptimizeExpression(Expression.Call( typeof(Queryable), nameof(Queryable.SelectMany), - new[] { source.ElementType, resultType }, + [source.ElementType, resultType!], source.Expression, Expression.Quote(lambda)) ); @@ -2130,7 +2130,7 @@ public static dynamic Single(this IQueryable source) Check.NotNull(source); var optimized = OptimizeExpression(Expression.Call(typeof(Queryable), nameof(Queryable.Single), new[] { source.ElementType }, source.Expression)); - return source.Provider.Execute(optimized); + return source.Provider.Execute(optimized)!; } /// diff --git a/src/System.Linq.Dynamic.Core/DynamicQueryableWithFormattableStringExtensions.cs b/src/System.Linq.Dynamic.Core/DynamicQueryableWithFormattableStringExtensions.cs index 8cc57e0b..76185707 100644 --- a/src/System.Linq.Dynamic.Core/DynamicQueryableWithFormattableStringExtensions.cs +++ b/src/System.Linq.Dynamic.Core/DynamicQueryableWithFormattableStringExtensions.cs @@ -16,412 +16,412 @@ public static class DynamicQueryableWithFormattableStringExtensions { private static readonly Regex ReplaceArgumentsRegex = new(@"{(\d+)}", RegexOptions.Compiled); - public static IQueryable WhereInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static IQueryable WhereInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Where(source, config, predicateStr, args); } - public static IQueryable WhereInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static IQueryable WhereInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Where(source, predicateStr, args); } - public static IQueryable WhereInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static IQueryable WhereInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Where(source, config, predicateStr, args); } - public static IQueryable WhereInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static IQueryable WhereInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Where(source, predicateStr, args); } [PublicAPI] - public static bool AllInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static bool AllInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.All(source, predicateStr, args); } [PublicAPI] - public static bool AllInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static bool AllInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.All(source, config, predicateStr, args); } [PublicAPI] - public static bool AnyInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static bool AnyInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Any(source, config, predicateStr, args); } - public static bool AnyInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static bool AnyInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Any(source, predicateStr, args); } [PublicAPI] - public static double AverageInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static double AverageInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Average(source, config, predicateStr, args); } [PublicAPI] - public static double AverageInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static double AverageInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Average(source, predicateStr, args); } - public static dynamic SingleInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static dynamic SingleInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Single(source, config, predicateStr, args); } - public static dynamic SingleInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static dynamic SingleInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Single(source, predicateStr, args); } - public static dynamic SingleOrDefaultInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static dynamic SingleOrDefaultInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.SingleOrDefault(source, config, predicateStr, args); } - public static dynamic SingleOrDefaultInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static dynamic SingleOrDefaultInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.SingleOrDefault(source, predicateStr, args); } - public static IQueryable SkipWhileInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static IQueryable SkipWhileInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.SkipWhile(source, config, predicateStr, args); } - public static IQueryable SkipWhileInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static IQueryable SkipWhileInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.SkipWhile(source, predicateStr, args); } - public static IQueryable TakeWhileInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static IQueryable TakeWhileInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.TakeWhile(source, config, predicateStr, args); } - public static IQueryable TakeWhileInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static IQueryable TakeWhileInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.TakeWhile(source, predicateStr, args); } [PublicAPI] - public static object SumInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static object SumInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Sum(source, config, predicateStr, args); } [PublicAPI] - public static object SumInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static object SumInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Sum(source, predicateStr, args); } [PublicAPI] - public static int CountInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static int CountInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Count(source, config, predicateStr, args); } - public static int CountInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static int CountInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Count(source, predicateStr, args); } - public static dynamic FirstInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static dynamic FirstInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.First(source, config, predicateStr, args); } - public static dynamic FirstInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static dynamic FirstInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.First(source, predicateStr, args); } - public static dynamic FirstOrDefaultInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static dynamic FirstOrDefaultInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.FirstOrDefault(source, config, predicateStr, args); } - public static dynamic FirstOrDefaultInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static dynamic FirstOrDefaultInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.FirstOrDefault(source, predicateStr, args); } - public static dynamic LastInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static dynamic LastInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Last(source, config, predicateStr, args); } - public static dynamic LastInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static dynamic LastInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Last(source, predicateStr, args); } - public static dynamic LastOrDefaultInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static dynamic LastOrDefaultInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.LastOrDefault(source, config, predicateStr, args); } - public static dynamic LastOrDefaultInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static dynamic LastOrDefaultInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.LastOrDefault(source, predicateStr, args); } [PublicAPI] - public static long LongCountInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static long LongCountInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.LongCount(source, config, predicateStr, args); } - public static long LongCountInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static long LongCountInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.LongCount(source, predicateStr, args); } [PublicAPI] - public static object MaxInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static object MaxInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Max(source, config, predicateStr, args); } [PublicAPI] - public static object MaxInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static object MaxInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Max(source, predicateStr, args); } [PublicAPI] - public static object MinInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString predicate) + public static object MinInterpolated(this IQueryable source, ParsingConfig config, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Min(source, config, predicateStr, args); } [PublicAPI] - public static object MinInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString predicate) + public static object MinInterpolated(this IQueryable source, FormattableString predicate) { var predicateStr = ParseFormattableString(predicate, out var args); return DynamicQueryableExtensions.Min(source, predicateStr, args); } - public static IQueryable SelectInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString selector) + public static IQueryable SelectInterpolated(this IQueryable source, ParsingConfig config, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.Select(source, config, selectorStr, args); } - public static IQueryable SelectInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] Type resultType, [NotNull] FormattableString selector) + public static IQueryable SelectInterpolated(this IQueryable source, ParsingConfig config, Type resultType, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.Select(source, config, resultType, selectorStr, args); } - public static IQueryable SelectInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString selector) + public static IQueryable SelectInterpolated(this IQueryable source, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.Select(source, selectorStr, args); } - public static IQueryable SelectInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString selector) + public static IQueryable SelectInterpolated(this IQueryable source, ParsingConfig config, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.Select(source, config, selectorStr, args); } - public static IQueryable SelectInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString selector) + public static IQueryable SelectInterpolated(this IQueryable source, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.Select(source, selectorStr, args); } - public static IQueryable SelectManyInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString collectionSelector, [NotNull] FormattableString resultSelector) + public static IQueryable SelectManyInterpolated(this IQueryable source, ParsingConfig config, FormattableString collectionSelector, FormattableString resultSelector) { var collectionSelectorStr = ParseFormattableString(collectionSelector, out var collectionSelectorArgs); var resultSelectorStr = ParseFormattableString(resultSelector, out var resultSelectorArgs); return DynamicQueryableExtensions.SelectMany(source, config, collectionSelectorStr, resultSelectorStr, collectionSelectorArgs, resultSelectorArgs); } - public static IQueryable SelectManyInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString selector) + public static IQueryable SelectManyInterpolated(this IQueryable source, ParsingConfig config, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.SelectMany(source, config, selectorStr, args); } - public static IQueryable SelectManyInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] Type resultType, [NotNull] FormattableString selector) + public static IQueryable SelectManyInterpolated(this IQueryable source, ParsingConfig config, Type resultType, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.SelectMany(source, config, resultType, selectorStr, args); } - public static IQueryable SelectManyInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString collectionSelector, [NotNull] FormattableString resultSelector) + public static IQueryable SelectManyInterpolated(this IQueryable source, FormattableString collectionSelector, FormattableString resultSelector) { var collectionSelectorStr = ParseFormattableString(collectionSelector, out var collectionSelectorArgs); var resultSelectorStr = ParseFormattableString(resultSelector, out var resultSelectorArgs); return DynamicQueryableExtensions.SelectMany(source, collectionSelectorStr, collectionSelectorArgs, resultSelectorStr, resultSelectorArgs); } - public static IQueryable SelectManyInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString selector) + public static IQueryable SelectManyInterpolated(this IQueryable source, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.SelectMany(source, selectorStr, args); } - public static IQueryable SelectManyInterpolated([NotNull] this IQueryable source, [NotNull] Type resultType, [NotNull] FormattableString selector) + public static IQueryable SelectManyInterpolated(this IQueryable source, Type resultType, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.SelectMany(source, resultType, selectorStr, args); } - public static IQueryable SelectManyInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString selector) + public static IQueryable SelectManyInterpolated(this IQueryable source, ParsingConfig config, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.SelectMany(source, config, selectorStr, args); } - public static IQueryable SelectManyInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString selector) + public static IQueryable SelectManyInterpolated(this IQueryable source, FormattableString selector) { var selectorStr = ParseFormattableString(selector, out var args); return DynamicQueryableExtensions.SelectMany(source, selectorStr, args); } - public static IOrderedQueryable OrderByInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString ordering, IComparer comparer) + public static IOrderedQueryable OrderByInterpolated(this IQueryable source, ParsingConfig config, FormattableString ordering, IComparer comparer) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.OrderBy(source, config, orderingStr, comparer, args); } - public static IOrderedQueryable OrderByInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString ordering) + public static IOrderedQueryable OrderByInterpolated(this IQueryable source, ParsingConfig config, FormattableString ordering) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.OrderBy(source, config, orderingStr, args); } - public static IOrderedQueryable OrderByInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString ordering, IComparer comparer) + public static IOrderedQueryable OrderByInterpolated(this IQueryable source, FormattableString ordering, IComparer comparer) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.OrderBy(source, orderingStr, comparer, args); } - public static IOrderedQueryable OrderByInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString ordering) + public static IOrderedQueryable OrderByInterpolated(this IQueryable source, FormattableString ordering) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.OrderBy(source, orderingStr, args); } - public static IOrderedQueryable OrderByInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString ordering, IComparer comparer) + public static IOrderedQueryable OrderByInterpolated(this IQueryable source, ParsingConfig config, FormattableString ordering, IComparer comparer) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.OrderBy(source, config, orderingStr, comparer, args); } - public static IOrderedQueryable OrderByInterpolated([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString ordering) + public static IOrderedQueryable OrderByInterpolated(this IQueryable source, ParsingConfig config, FormattableString ordering) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.OrderBy(source, config, orderingStr, args); } - public static IOrderedQueryable OrderByInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString ordering, IComparer comparer) + public static IOrderedQueryable OrderByInterpolated(this IQueryable source, FormattableString ordering, IComparer comparer) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.OrderBy(source, orderingStr, comparer, args); } - public static IOrderedQueryable OrderByInterpolated([NotNull] this IQueryable source, [NotNull] FormattableString ordering) + public static IOrderedQueryable OrderByInterpolated(this IQueryable source, FormattableString ordering) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.OrderBy(source, orderingStr, args); } - public static IOrderedQueryable ThenByInterpolated([NotNull] this IOrderedQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString ordering, IComparer comparer) + public static IOrderedQueryable ThenByInterpolated(this IOrderedQueryable source, ParsingConfig config, FormattableString ordering, IComparer comparer) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.ThenBy(source, config, orderingStr, comparer, args); } - public static IOrderedQueryable ThenByInterpolated([NotNull] this IOrderedQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString ordering) + public static IOrderedQueryable ThenByInterpolated(this IOrderedQueryable source, ParsingConfig config, FormattableString ordering) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.ThenBy(source, config, orderingStr, args); } - public static IOrderedQueryable ThenByInterpolated([NotNull] this IOrderedQueryable source, [NotNull] FormattableString ordering, IComparer comparer) + public static IOrderedQueryable ThenByInterpolated(this IOrderedQueryable source, FormattableString ordering, IComparer comparer) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.ThenBy(source, orderingStr, comparer, args); } - public static IOrderedQueryable ThenByInterpolated([NotNull] this IOrderedQueryable source, [NotNull] FormattableString ordering) + public static IOrderedQueryable ThenByInterpolated(this IOrderedQueryable source, FormattableString ordering) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.ThenBy(source, orderingStr, args); } - public static IOrderedQueryable ThenByInterpolated([NotNull] this IOrderedQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString ordering, IComparer comparer) + public static IOrderedQueryable ThenByInterpolated(this IOrderedQueryable source, ParsingConfig config, FormattableString ordering, IComparer comparer) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.ThenBy(source, config, orderingStr, comparer, args); } - public static IOrderedQueryable ThenByInterpolated([NotNull] this IOrderedQueryable source, [NotNull] ParsingConfig config, [NotNull] FormattableString ordering) + public static IOrderedQueryable ThenByInterpolated(this IOrderedQueryable source, ParsingConfig config, FormattableString ordering) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.ThenBy(source, config, orderingStr, args); } - public static IOrderedQueryable ThenByInterpolated([NotNull] this IOrderedQueryable source, [NotNull] FormattableString ordering, IComparer comparer) + public static IOrderedQueryable ThenByInterpolated(this IOrderedQueryable source, FormattableString ordering, IComparer comparer) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.ThenBy(source, orderingStr, comparer, args); } - public static IOrderedQueryable ThenByInterpolated([NotNull] this IOrderedQueryable source, [NotNull] FormattableString ordering) + public static IOrderedQueryable ThenByInterpolated(this IOrderedQueryable source, FormattableString ordering) { var orderingStr = ParseFormattableString(ordering, out var args); return DynamicQueryableExtensions.ThenBy(source, orderingStr, args); } - private static string ParseFormattableString(FormattableString predicate, out object[] args) + private static string ParseFormattableString(FormattableString predicate, out object?[] args) { args = predicate.GetArguments(); return ReplaceArgumentsRegex.Replace(predicate.Format, "@$1"); // replace {0} with @0 diff --git a/src/System.Linq.Dynamic.Core/ExtensibilityPoint.cs b/src/System.Linq.Dynamic.Core/ExtensibilityPoint.cs index 45abdebd..96c46bdf 100644 --- a/src/System.Linq.Dynamic.Core/ExtensibilityPoint.cs +++ b/src/System.Linq.Dynamic.Core/ExtensibilityPoint.cs @@ -12,5 +12,5 @@ public class ExtensibilityPoint /// Place to optimize your queries. Example: Add a reference to Nuget package Linq.Expression.Optimizer /// and in your program initializers set Extensibility.QueryOptimizer = ExpressionOptimizer.visit; /// - public static Func QueryOptimizer = e => e; + public static Func? QueryOptimizer = e => e; } \ No newline at end of file diff --git a/src/System.Linq.Dynamic.Core/Parser/ConstantExpressionWrapper.cs b/src/System.Linq.Dynamic.Core/Parser/ConstantExpressionWrapper.cs index 18c6e4ca..97db08bc 100644 --- a/src/System.Linq.Dynamic.Core/Parser/ConstantExpressionWrapper.cs +++ b/src/System.Linq.Dynamic.Core/Parser/ConstantExpressionWrapper.cs @@ -17,7 +17,7 @@ public void Wrap(ref Expression expression) { if (constantExpression.Type == typeof(bool)) { - expression = Wrap((bool)constantExpression.Value); + expression = Wrap((bool)constantExpression.Value!); } else if (constantExpression.Type == typeof(bool?)) { @@ -25,7 +25,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(char)) { - expression = Wrap((char)constantExpression.Value); + expression = Wrap((char)constantExpression.Value!); } else if (constantExpression.Type == typeof(char?)) { @@ -33,7 +33,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(byte)) { - expression = Wrap((byte)constantExpression.Value); + expression = Wrap((byte)constantExpression.Value!); } else if (constantExpression.Type == typeof(byte?)) { @@ -41,15 +41,19 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(sbyte)) { - expression = Wrap((sbyte)constantExpression.Value); + expression = Wrap((sbyte)constantExpression.Value!); + } + else if (constantExpression.Type == typeof(sbyte?)) + { + expression = Wrap((sbyte?)constantExpression.Value); } else if (constantExpression.Type == typeof(string)) { - expression = Wrap((string)constantExpression.Value); + expression = Wrap((string?)constantExpression.Value); } else if (constantExpression.Type == typeof(float)) { - expression = Wrap((float)constantExpression.Value); + expression = Wrap((float)constantExpression.Value!); } else if (constantExpression.Type == typeof(float?)) { @@ -57,7 +61,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(decimal)) { - expression = Wrap((decimal)constantExpression.Value); + expression = Wrap((decimal)constantExpression.Value!); } else if (constantExpression.Type == typeof(decimal?)) { @@ -65,7 +69,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(double)) { - expression = Wrap((double)constantExpression.Value); + expression = Wrap((double)constantExpression.Value!); } else if (constantExpression.Type == typeof(double?)) { @@ -73,7 +77,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(long)) { - expression = Wrap((long)constantExpression.Value); + expression = Wrap((long)constantExpression.Value!); } else if (constantExpression.Type == typeof(long?)) { @@ -81,7 +85,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(ulong)) { - expression = Wrap((ulong)constantExpression.Value); + expression = Wrap((ulong)constantExpression.Value!); } else if (constantExpression.Type == typeof(ulong?)) { @@ -89,7 +93,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(int)) { - expression = Wrap((int)constantExpression.Value); + expression = Wrap((int)constantExpression.Value!); } else if (constantExpression.Type == typeof(int?)) { @@ -97,7 +101,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(uint)) { - expression = Wrap((uint)constantExpression.Value); + expression = Wrap((uint)constantExpression.Value!); } else if (constantExpression.Type == typeof(uint?)) { @@ -105,7 +109,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(short)) { - expression = Wrap((short)constantExpression.Value); + expression = Wrap((short)constantExpression.Value!); } else if (constantExpression.Type == typeof(short?)) { @@ -113,7 +117,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(ushort)) { - expression = Wrap((ushort)constantExpression.Value); + expression = Wrap((ushort)constantExpression.Value!); } else if (constantExpression.Type == typeof(ushort?)) { @@ -121,7 +125,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(Guid)) { - expression = Wrap((Guid)constantExpression.Value); + expression = Wrap((Guid)constantExpression.Value!); } else if (constantExpression.Type == typeof(Guid?)) { @@ -129,7 +133,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(DateTime)) { - expression = Wrap((DateTime)constantExpression.Value); + expression = Wrap((DateTime)constantExpression.Value!); } else if (constantExpression.Type == typeof(DateTime?)) { @@ -137,7 +141,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(DateTimeOffset)) { - expression = Wrap((DateTimeOffset)constantExpression.Value); + expression = Wrap((DateTimeOffset)constantExpression.Value!); } else if (constantExpression.Type == typeof(DateTimeOffset?)) { @@ -145,7 +149,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(TimeSpan)) { - expression = Wrap((TimeSpan)constantExpression.Value); + expression = Wrap((TimeSpan)constantExpression.Value!); } else if (constantExpression.Type == typeof(TimeSpan?)) { @@ -154,7 +158,7 @@ public void Wrap(ref Expression expression) #if NET6_0_OR_GREATER else if (constantExpression.Type == typeof(DateOnly)) { - expression = Wrap((DateOnly)constantExpression.Value); + expression = Wrap((DateOnly)constantExpression.Value!); } else if (constantExpression.Type == typeof(DateOnly?)) { @@ -162,7 +166,7 @@ public void Wrap(ref Expression expression) } else if (constantExpression.Type == typeof(TimeOnly)) { - expression = Wrap((TimeOnly)constantExpression.Value); + expression = Wrap((TimeOnly)constantExpression.Value!); } else if (constantExpression.Type == typeof(TimeOnly?)) { diff --git a/src/System.Linq.Dynamic.Core/Parser/EnumerationsFromMscorlib.cs b/src/System.Linq.Dynamic.Core/Parser/EnumerationsFromMscorlib.cs index b2590f18..6b2f4b25 100644 --- a/src/System.Linq.Dynamic.Core/Parser/EnumerationsFromMscorlib.cs +++ b/src/System.Linq.Dynamic.Core/Parser/EnumerationsFromMscorlib.cs @@ -13,7 +13,7 @@ internal static class EnumerationsFromMscorlib static EnumerationsFromMscorlib() { - var list = new List(AddEnumsFromAssembly(typeof(UriFormat).GetTypeInfo().Assembly.FullName)); + var list = new List(AddEnumsFromAssembly(typeof(UriFormat).GetTypeInfo().Assembly.FullName!)); #if !(UAP10_0 || NETSTANDARD || NET35 || NETCOREAPP) list.AddRange(AddEnumsFromAssembly("mscorlib")); @@ -45,13 +45,13 @@ private static void Add(IGrouping group) { var singleType = group.Single(); PredefinedEnumerationTypes.Add(group.Key, singleType); - PredefinedEnumerationTypes.Add(singleType.FullName, singleType); + PredefinedEnumerationTypes.Add(singleType.FullName!, singleType); } else { foreach (var fullType in group) { - PredefinedEnumerationTypes.Add(fullType.FullName, fullType); + PredefinedEnumerationTypes.Add(fullType.FullName!, fullType); } } } diff --git a/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs b/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs index 09f2a191..c3752235 100644 --- a/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs +++ b/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs @@ -223,7 +223,7 @@ internal IList ParseOrdering(bool forceThenBy = false) methodName = ascending ? MethodOrderBy : MethodOrderByDescending; } - orderings.Add(new DynamicOrdering { Selector = expr, Ascending = ascending, MethodName = methodName }); + orderings.Add(new DynamicOrdering(expr, ascending, methodName)); if (_textParser.CurrentToken.Id != TokenId.Comma) { diff --git a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IAddSignatures.cs b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IAddSignatures.cs index 15c398f9..028039c9 100644 --- a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IAddSignatures.cs +++ b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IAddSignatures.cs @@ -1,5 +1,8 @@ -namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; +using System.Diagnostics.CodeAnalysis; +namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; + +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] internal interface IAddSignatures : IArithmeticSignatures { void F(TimeSpan x, TimeSpan y); diff --git a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IArithmeticSignatures.cs b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IArithmeticSignatures.cs index 1021c660..2133dd2f 100644 --- a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IArithmeticSignatures.cs +++ b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IArithmeticSignatures.cs @@ -1,5 +1,8 @@ -namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; +using System.Diagnostics.CodeAnalysis; +namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; + +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] internal interface IArithmeticSignatures { void F(int x, int y); diff --git a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IEqualitySignatures.cs b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IEqualitySignatures.cs index e7c79c31..d76a3da5 100644 --- a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IEqualitySignatures.cs +++ b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IEqualitySignatures.cs @@ -1,5 +1,8 @@ -namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; +using System.Diagnostics.CodeAnalysis; +namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; + +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] internal interface IEqualitySignatures : IRelationalSignatures { void F(bool x, bool y); diff --git a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/ILogicalSignatures.cs b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/ILogicalSignatures.cs index b3724b39..6a535232 100644 --- a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/ILogicalSignatures.cs +++ b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/ILogicalSignatures.cs @@ -1,8 +1,10 @@ -namespace System.Linq.Dynamic.Core.Parser.SupportedOperands +using System.Diagnostics.CodeAnalysis; + +namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; + +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] +internal interface ILogicalSignatures { - internal interface ILogicalSignatures - { - void F(bool x, bool y); - void F(bool? x, bool? y); - } -} + void F(bool x, bool y); + void F(bool? x, bool? y); +} \ No newline at end of file diff --git a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/INegationSignatures.cs b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/INegationSignatures.cs index 6d9c97d5..b05410c0 100644 --- a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/INegationSignatures.cs +++ b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/INegationSignatures.cs @@ -1,16 +1,18 @@ -namespace System.Linq.Dynamic.Core.Parser.SupportedOperands +using System.Diagnostics.CodeAnalysis; + +namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; + +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] +internal interface INegationSignatures { - internal interface INegationSignatures - { - void F(int x); - void F(long x); - void F(float x); - void F(double x); - void F(decimal x); - void F(int? x); - void F(long? x); - void F(float? x); - void F(double? x); - void F(decimal? x); - } -} + void F(int x); + void F(long x); + void F(float x); + void F(double x); + void F(decimal x); + void F(int? x); + void F(long? x); + void F(float? x); + void F(double? x); + void F(decimal? x); +} \ No newline at end of file diff --git a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/INotSignatures.cs b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/INotSignatures.cs index 2c53f724..0e6f0b8a 100644 --- a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/INotSignatures.cs +++ b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/INotSignatures.cs @@ -1,8 +1,10 @@ -namespace System.Linq.Dynamic.Core.Parser.SupportedOperands +using System.Diagnostics.CodeAnalysis; + +namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; + +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] +internal interface INotSignatures { - internal interface INotSignatures - { - void F(bool x); - void F(bool? x); - } -} + void F(bool x); + void F(bool? x); +} \ No newline at end of file diff --git a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IRelationalSignatures.cs b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IRelationalSignatures.cs index 8c1c9c1b..c7556e5c 100644 --- a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IRelationalSignatures.cs +++ b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IRelationalSignatures.cs @@ -1,5 +1,8 @@ -namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; +using System.Diagnostics.CodeAnalysis; +namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; + +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] internal interface IRelationalSignatures : IArithmeticSignatures { void F(string x, string y); diff --git a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IShiftSignatures.cs b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IShiftSignatures.cs index f9e68e8b..c42f2ff2 100644 --- a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IShiftSignatures.cs +++ b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/IShiftSignatures.cs @@ -1,22 +1,24 @@ -namespace System.Linq.Dynamic.Core.Parser.SupportedOperands +using System.Diagnostics.CodeAnalysis; + +namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; + +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] +internal interface IShiftSignatures { - internal interface IShiftSignatures - { - void F(int x, int y); - void F(uint x, int y); - void F(long x, int y); - void F(ulong x, int y); - void F(int? x, int y); - void F(uint? x, int y); - void F(long? x, int y); - void F(ulong? x, int y); - void F(int x, int? y); - void F(uint x, int? y); - void F(long x, int? y); - void F(ulong x, int? y); - void F(int? x, int? y); - void F(uint? x, int? y); - void F(long? x, int? y); - void F(ulong? x, int? y); - } -} + void F(int x, int y); + void F(uint x, int y); + void F(long x, int y); + void F(ulong x, int y); + void F(int? x, int y); + void F(uint? x, int y); + void F(long? x, int y); + void F(ulong? x, int y); + void F(int x, int? y); + void F(uint x, int? y); + void F(long x, int? y); + void F(ulong x, int? y); + void F(int? x, int? y); + void F(uint? x, int? y); + void F(long? x, int? y); + void F(ulong? x, int? y); +} \ No newline at end of file diff --git a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/ISubtractSignatures.cs b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/ISubtractSignatures.cs index c46b22ce..011993ac 100644 --- a/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/ISubtractSignatures.cs +++ b/src/System.Linq.Dynamic.Core/Parser/SupportedOperands/ISubtractSignatures.cs @@ -1,5 +1,8 @@ -namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; +using System.Diagnostics.CodeAnalysis; +namespace System.Linq.Dynamic.Core.Parser.SupportedOperands; + +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] internal interface ISubtractSignatures : IAddSignatures { void F(DateTime x, DateTime y); diff --git a/src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs b/src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs index 526b5c4b..2cf80b2f 100644 --- a/src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs +++ b/src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Linq.Dynamic.Core.Validation; using System.Reflection; namespace System.Linq.Dynamic.Core.Parser; @@ -325,22 +324,16 @@ public static bool IsNumericType(Type type) public static bool IsNullableType(Type type) { - Check.NotNull(type, nameof(type)); - return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); } public static bool TypeCanBeNull(Type type) { - Check.NotNull(type); - return !type.GetTypeInfo().IsValueType || IsNullableType(type); } public static Type ToNullableType(Type type) { - Check.NotNull(type); - if (IsNullableType(type)) { // Already nullable, just return the type. @@ -440,15 +433,11 @@ public static Type GetNullableType(Type type) public static Type GetNonNullableType(Type type) { - Check.NotNull(type, nameof(type)); - return IsNullableType(type) ? type.GetTypeInfo().GetGenericTypeArguments()[0] : type; } public static Type GetUnderlyingType(Type type) { - Check.NotNull(type, nameof(type)); - Type[] genericTypeArguments = type.GetGenericArguments(); if (genericTypeArguments.Any()) { @@ -461,8 +450,6 @@ public static Type GetUnderlyingType(Type type) public static IList GetSelfAndBaseTypes(Type type, bool excludeObject = false) { - Check.NotNull(type); - if (type.GetTypeInfo().IsInterface) { var types = new List(); diff --git a/src/System.Linq.Dynamic.Core/System.Linq.Dynamic.Core.csproj b/src/System.Linq.Dynamic.Core/System.Linq.Dynamic.Core.csproj index ab7bced3..94563ed7 100644 --- a/src/System.Linq.Dynamic.Core/System.Linq.Dynamic.Core.csproj +++ b/src/System.Linq.Dynamic.Core/System.Linq.Dynamic.Core.csproj @@ -11,6 +11,7 @@ {D3804228-91F4-4502-9595-39584E510002} net35;net40;net45;net452;net46;netstandard1.3;netstandard2.0;netstandard2.1;uap10.0;netcoreapp2.1;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 1.4.$(PatchVersion) + diff --git a/src/System.Linq.Dynamic.Core/Util/ParameterExpressionRenamer.cs b/src/System.Linq.Dynamic.Core/Util/ParameterExpressionRenamer.cs index 0c6dc034..47e5a88c 100644 --- a/src/System.Linq.Dynamic.Core/Util/ParameterExpressionRenamer.cs +++ b/src/System.Linq.Dynamic.Core/Util/ParameterExpressionRenamer.cs @@ -59,7 +59,6 @@ protected override Expression VisitParameter(ParameterExpression node) } return _parameterExpression; - // throw new InvalidOperationException($"The {nameof(ParameterExpressionRenamer)} can only rename 1 (Typed)ParameterExpression in an Expression."); } return node; diff --git a/src/System.Linq.Dynamic.Core/Validation/Check.cs b/src/System.Linq.Dynamic.Core/Validation/Check.cs index a1c36983..00994c89 100644 --- a/src/System.Linq.Dynamic.Core/Validation/Check.cs +++ b/src/System.Linq.Dynamic.Core/Validation/Check.cs @@ -10,7 +10,7 @@ internal static class Check { public static T Condition(T value, Predicate predicate, [CallerArgumentExpression("value")] string? parameterName = null) { - NotNull(predicate, nameof(predicate)); + NotNull(predicate); if (!predicate(value)) { @@ -104,7 +104,7 @@ public static IEnumerable HasNoNulls(IEnumerable value, [CallerArgument { if (value is null) { - NotNullOrEmpty(parameterName, nameof(parameterName)); + NotNullOrEmpty(parameterName); throw new ArgumentNullException(parameterName); } @@ -112,7 +112,7 @@ public static IEnumerable HasNoNulls(IEnumerable value, [CallerArgument // ReSharper disable once PossibleMultipleEnumeration if (value.Any(v => v is null)) { - NotNullOrEmpty(parameterName, nameof(parameterName)); + NotNullOrEmpty(parameterName); throw new ArgumentException(parameterName); } diff --git a/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs b/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs index 055216a1..a0895cc5 100644 --- a/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs +++ b/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs @@ -26,18 +26,18 @@ public class Foo { public Foo FooValue { get; set; } - public string Zero() => null; + public string Zero() => string.Empty; - public string One(int x) => null; + public string One(int x) => string.Empty; - public string Two(int x, int y) => null; + public string Two(int x, int y) => string.Empty; } private class MyClass { - public List MyStrings { get; set; } + public List MyStrings { get; set; } = null!; - public List MyClasses { get; set; } + public List MyClasses { get; set; } = null!; public int Foo() { @@ -53,7 +53,7 @@ public void Bar() public string? Description { get; set; } - public MyClass Child { get; set; } + public MyClass Child { get; set; } = null!; } private class MyClassCustomTypeProvider : DefaultDynamicLinqCustomTypeProvider @@ -299,7 +299,7 @@ public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQu // Assert ConstantExpression constantExpression = (ConstantExpression)((BinaryExpression)expression.Body).Right; - object value = constantExpression.Value; + var value = constantExpression.Value; Check.That(value).IsEqualTo("x"); } @@ -318,7 +318,7 @@ public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQu // Assert ConstantExpression constantExpression = (ConstantExpression)((BinaryExpression)expression.Body).Right; - object value = constantExpression.Value; + var value = constantExpression.Value; Check.That(value).IsEqualTo(new DateTime(2022, 3, 2)); } @@ -339,11 +339,11 @@ public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQu // Assert expressionAsString.Should().Be("Param_0 => (Param_0.Id == value(System.Linq.Dynamic.Core.Parser.WrappedValue`1[System.Int32]).Value)"); - ConstantExpression constantExpression = (ConstantExpression)((MemberExpression)((BinaryExpression)expression.Body).Right).Expression; - var wrappedObj = constantExpression!.Value; + ConstantExpression constantExpression = (ConstantExpression)((MemberExpression)((BinaryExpression)expression.Body).Right).Expression!; + var wrappedObj = constantExpression.Value!; - PropertyInfo propertyInfo = wrappedObj!.GetType().GetProperty("Value", BindingFlags.Instance | BindingFlags.Public); - object value = propertyInfo!.GetValue(wrappedObj); + PropertyInfo propertyInfo = wrappedObj.GetType().GetProperty("Value", BindingFlags.Instance | BindingFlags.Public)!; + var value = propertyInfo.GetValue(wrappedObj); Check.That(value).IsEqualTo(42); } @@ -638,7 +638,7 @@ public void DynamicExpressionParser_ParseLambda_Issue58() { // Arrange var customTypeProvider = new Mock(); - customTypeProvider.Setup(c => c.GetCustomTypes()).Returns(new HashSet { typeof(MyClass) }); + customTypeProvider.Setup(c => c.GetCustomTypes()).Returns([typeof(MyClass)]); var config = new ParsingConfig { CustomTypeProvider = customTypeProvider.Object @@ -744,28 +744,28 @@ public void DynamicExpressionParser_ParseLambda_ParameterName_Null() public void DynamicExpressionParser_ParseLambda_ParameterExpressionMethodCall_ReturnsIntExpression() { var expression = DynamicExpressionParser.ParseLambda(true, - new[] { Expression.Parameter(typeof(int), "x") }, + [Expression.Parameter(typeof(int), "x")], typeof(int), "x + 1"); Assert.Equal(typeof(int), expression.Body.Type); } - public static object[][] Decimals() + public static IEnumerable Decimals() { - return new object[][] - { - new object[] { "de-DE", "1m", 1f }, - new object[] { "de-DE", "-42,0m", -42m }, - new object[] { "de-DE", "3,215m", 3.215m }, + return + [ + ["de-DE", "1m", 1f], + ["de-DE", "-42,0m", -42m], + ["de-DE", "3,215m", 3.215m], - new object[] { null, "1m", 1f }, - new object[] { null, "-42.0m", -42m }, - new object[] { null, "3.215m", 3.215m } - }; + [null, "1m", 1f], + [null, "-42.0m", -42m], + [null, "3.215m", 3.215m] + ]; } [Theory] [MemberData(nameof(Decimals))] - public void DynamicExpressionParser_ParseLambda_Decimal(string culture, string expression, decimal expected) + public void DynamicExpressionParser_ParseLambda_Decimal(string? culture, string expression, decimal expected) { // Arrange var config = new ParsingConfig(); @@ -784,24 +784,24 @@ public void DynamicExpressionParser_ParseLambda_Decimal(string culture, string e result.Should().Be(expected); } - public static object[][] Floats() + public static IEnumerable Floats() { - return new object[][] - { - new object[] { "de-DE", "1F", 1f }, - new object[] { "de-DE", "1f", 1f }, - new object[] { "de-DE", "-42f", -42d }, - new object[] { "de-DE", "3,215f", 3.215d }, + return + [ + ["de-DE", "1F", 1f], + ["de-DE", "1f", 1f], + ["de-DE", "-42f", -42d], + ["de-DE", "3,215f", 3.215d], - new object[] { null, "1F", 1f }, - new object[] { null, "1f", 1f }, - new object[] { null, "-42f", -42d }, - new object[] { null, "3.215f", 3.215d }, - }; + [null, "1F", 1f], + [null, "1f", 1f], + [null, "-42f", -42d], + [null, "3.215f", 3.215d], + ]; } [Theory] [MemberData(nameof(Floats))] - public void DynamicExpressionParser_ParseLambda_Float(string culture, string expression, float expected) + public void DynamicExpressionParser_ParseLambda_Float(string? culture, string expression, float expected) { // Arrange var config = new ParsingConfig(); @@ -820,28 +820,28 @@ public void DynamicExpressionParser_ParseLambda_Float(string culture, string exp result.Should().Be(expected); } - public static IEnumerable Doubles() + public static IEnumerable Doubles() { - return new object[][] - { - new object[] { "de-DE", "1D", 1d }, - new object[] { "de-DE", "1d", 1d }, - new object[] { "de-DE", "-42d", -42d }, - new object[] { "de-DE", "3,215d", 3.215d }, - new object[] { "de-DE", "1,2345E-4", 0.00012345d }, - new object[] { "de-DE", "1,2345E4", 12345d }, + return + [ + ["de-DE", "1D", 1d], + ["de-DE", "1d", 1d], + ["de-DE", "-42d", -42d], + ["de-DE", "3,215d", 3.215d], + ["de-DE", "1,2345E-4", 0.00012345d], + ["de-DE", "1,2345E4", 12345d], - new object[] { null, "1D", 1d }, - new object[] { null, "1d", 1d }, - new object[] { null, "-42d", -42d }, - new object[] { null, "3.215d", 3.215d }, - new object[] { null, "1.2345E-4", 0.00012345d }, - new object[] { null, "1.2345E4", 12345d } - }; + [null, "1D", 1d], + [null, "1d", 1d], + [null, "-42d", -42d], + [null, "3.215d", 3.215d], + [null, "1.2345E-4", 0.00012345d], + [null, "1.2345E4", 12345d] + ]; } [Theory] [MemberData(nameof(Doubles))] - public void DynamicExpressionParser_ParseLambda_Double(string culture, string expression, double expected) + public void DynamicExpressionParser_ParseLambda_Double(string? culture, string expression, double expected) { // Arrange var config = new ParsingConfig(); @@ -862,7 +862,7 @@ public void DynamicExpressionParser_ParseLambda_Double(string culture, string ex public class EntityDbo { - public string Name { get; set; } + public string Name { get; set; } = string.Empty; } [Fact] @@ -893,7 +893,7 @@ public void DynamicExpressionParser_ParseLambda_StringLiteral_WithADot_In_Expres public void DynamicExpressionParser_ParseLambda_StringLiteral_ReturnsBooleanLambdaExpression() { var expression = DynamicExpressionParser.ParseLambda( - new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(bool), "Property1 == \"test\""); + [Expression.Parameter(typeof(string), "Property1")], typeof(bool), "Property1 == \"test\""); Assert.Equal(typeof(bool), expression.Body.Type); } @@ -901,16 +901,7 @@ public void DynamicExpressionParser_ParseLambda_StringLiteral_ReturnsBooleanLamb public void DynamicExpressionParser_ParseLambda_StringLiteralEmpty_ReturnsBooleanLambdaExpression() { var expression = DynamicExpressionParser.ParseLambda( - new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(bool), "Property1 == \"\""); - Assert.Equal(typeof(bool), expression.Body.Type); - } - - [Fact] - public void DynamicExpressionParser_ParseLambda_Config_StringLiteralEmpty_ReturnsBooleanLambdaExpression() - { - var config = new ParsingConfig(); - var expression = DynamicExpressionParser.ParseLambda( - new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(bool), "Property1 == \"\""); + [Expression.Parameter(typeof(string), "Property1")], typeof(bool), "Property1 == \"\""); Assert.Equal(typeof(bool), expression.Body.Type); } @@ -919,7 +910,7 @@ public void DynamicExpressionParser_ParseLambda_StringLiteralEmbeddedQuote_Retur { // Act var expression = DynamicExpressionParser.ParseLambda( - new[] { Expression.Parameter(typeof(string), "Property1") }, + [Expression.Parameter(typeof(string), "Property1")], typeof(bool), string.Format("Property1 == {0}", "\"test \\\"string\"")); @@ -967,7 +958,7 @@ public void DynamicExpressionParser_ParseLambda_StringLiteralStartEmbeddedQuote_ { // Act var expression = DynamicExpressionParser.ParseLambda( - new[] { Expression.Parameter(typeof(string), "Property1") }, + [Expression.Parameter(typeof(string), "Property1")], typeof(bool), string.Format("Property1 == {0}", "\"\\\"test\"")); @@ -997,7 +988,7 @@ public void DynamicExpressionParser_ParseLambda_StringLiteral_MissingClosingQuot var expectedRightValue = "\"test\\\""; Assert.Throws(() => DynamicExpressionParser.ParseLambda( - new[] { Expression.Parameter(typeof(string), "Property1") }, + [Expression.Parameter(typeof(string), "Property1")], typeof(bool), string.Format("Property1 == {0}", expectedRightValue))); } @@ -1007,7 +998,7 @@ public void DynamicExpressionParser_ParseLambda_StringLiteralEscapedBackslash_Re { // Act var expression = DynamicExpressionParser.ParseLambda( - new[] { Expression.Parameter(typeof(string), "Property1") }, + [Expression.Parameter(typeof(string), "Property1")], typeof(bool), string.Format("Property1 == {0}", "\"test\\\\string\"")); @@ -1021,7 +1012,7 @@ public void DynamicExpressionParser_ParseLambda_StringLiteralEscapedNewline_Retu { // Act var expression = DynamicExpressionParser.ParseLambda( - new[] { Expression.Parameter(typeof(string), "Property1") }, + [Expression.Parameter(typeof(string), "Property1")], typeof(bool), string.Format("Property1 == {0}", "\"test\\\\new\"")); @@ -1038,7 +1029,7 @@ public void DynamicExpressionParser_ParseLambda_StringLiteral_Backslash() //Act var expression = DynamicExpressionParser.ParseLambda( - new[] { Expression.Parameter(typeof(string), "Property1") }, + [Expression.Parameter(typeof(string), "Property1")], typeof(Boolean), string.Format("{0} >= {1}", "Property1.IndexOf(\"\\\\\")", expectedRightValue)); @@ -1056,13 +1047,13 @@ public void DynamicExpressionParser_ParseLambda_StringLiteral_QuotationMark() { var expectedRightValue = "0"; var expression = DynamicExpressionParser.ParseLambda( - new[] { Expression.Parameter(typeof(string), "Property1") }, - typeof(Boolean), + [Expression.Parameter(typeof(string), "Property1")], + typeof(bool), string.Format("{0} >= {1}", "Property1.IndexOf(\"\\\"\")", expectedRightValue)); var leftValue = ((BinaryExpression)expression.Body).Left.ToString(); var rightValue = ((BinaryExpression)expression.Body).Right.ToString(); - Assert.Equal(typeof(Boolean), expression.Body.Type); + Assert.Equal(typeof(bool), expression.Body.Type); Assert.Equal("Property1.IndexOf(\"\"\")", leftValue); Assert.Equal(expectedRightValue, rightValue); } @@ -1092,7 +1083,7 @@ public void DynamicExpressionParser_ParseLambda_CustomStaticMethod_WhenClassIsRe // Act var lambdaExpression = DynamicExpressionParser.ParseLambda(config, typeof(CustomClassWithStaticMethod), null, expression); var del = lambdaExpression.Compile(); - var result = (int)del.DynamicInvoke(context); + var result = (int?)del.DynamicInvoke(context); // Assert Check.That(result).IsEqualTo(10); @@ -1191,7 +1182,7 @@ public void DynamicExpressionParser_ParseLambda_With_Guid_Equals_Null() Assert.NotNull(boolLambda); var del = lambda.Compile(); - var result = (bool)del.DynamicInvoke(user); + var result = (bool)del.DynamicInvoke(user)!; // Assert Assert.True(result); @@ -1211,7 +1202,7 @@ public void DynamicExpressionParser_ParseLambda_With_Null_Equals_Guid() Assert.NotNull(boolLambda); var del = lambda.Compile(); - var result = (bool)del.DynamicInvoke(user); + var result = (bool)del.DynamicInvoke(user)!; // Assert Assert.True(result); @@ -1234,7 +1225,7 @@ public void DynamicExpressionParser_ParseLambda_With_DateTime_Equals_String() Assert.NotNull(dtLambda); var del = lambda.Compile(); - var result = (bool)del.DynamicInvoke(user); + var result = (bool?)del.DynamicInvoke(user); // Assert Assert.True(result); @@ -1248,7 +1239,7 @@ public void DynamicExpressionParser_ParseLambda_With_DateTime_UtcNow_AddHours_To var parameterExpression = Expression.Parameter(typeof(string)); // Act - var lambda = DynamicExpressionParser.ParseLambda(new[] { parameterExpression }, typeof(object), expressionText); + var lambda = DynamicExpressionParser.ParseLambda([parameterExpression], typeof(object), expressionText); // Assert lambda.Should().NotBeNull(); @@ -1273,7 +1264,7 @@ public void DynamicExpressionParser_ParseLambda_With_Guid_Equals_String() Assert.NotNull(guidLambda); var del = lambda.Compile(); - var result = (Guid)del.DynamicInvoke(user); + var result = (Guid?)del.DynamicInvoke(user); // Assert Assert.Equal(guidEmpty, result); @@ -1297,7 +1288,7 @@ public void DynamicExpressionParser_ParseLambda_With_Concat_String_CustomType() // Act 2 var del = lambda.Compile(); - var result = (string)del.DynamicInvoke(textHolder); + var result = (string?)del.DynamicInvoke(textHolder); // Assert 2 Assert.Equal("name1 (note1)", result); @@ -1321,7 +1312,7 @@ public void DynamicExpressionParser_ParseLambda_With_Concat_CustomType_String() // Act 2 var del = lambda.Compile(); - var result = (string)del.DynamicInvoke(textHolder); + var result = (string?)del.DynamicInvoke(textHolder); // Assert 2 Assert.Equal("note1 (name1)", result); @@ -1673,7 +1664,7 @@ public void DynamicExpressionParser_ParseLambda_StringEquals(string expressionAs var p0 = Expression.Parameter(typeof(string), "p0"); // Act - var expression = DynamicExpressionParser.ParseLambda(new[] { p0 }, typeof(bool), expressionAsString); + var expression = DynamicExpressionParser.ParseLambda([p0], typeof(bool), expressionAsString); var del = expression.Compile(); var result = del.DynamicInvoke(testValue) as bool?; @@ -1693,7 +1684,7 @@ public void DynamicExpressionParser_ParseLambda_StringEquals_WithCombinedConditi var u = Expression.Parameter(typeof(User), "u"); // Act - var expression = DynamicExpressionParser.ParseLambda(new[] { u }, typeof(bool), expressionAsString); + var expression = DynamicExpressionParser.ParseLambda([u], typeof(bool), expressionAsString); var del = expression.Compile(); var result = del.DynamicInvoke(new User { UserName = "testinG", Income = 10 }) as bool?; @@ -1784,7 +1775,7 @@ public void DynamicExpressionParser_ParseLambda_NullPropagation_InstanceMethod_0 { // Arrange var customTypeProvider = new Mock(); - customTypeProvider.Setup(c => c.GetCustomTypes()).Returns(new HashSet { typeof(Foo) }); + customTypeProvider.Setup(c => c.GetCustomTypes()).Returns([typeof(Foo)]); var config = new ParsingConfig { CustomTypeProvider = customTypeProvider.Object @@ -1807,7 +1798,7 @@ public void DynamicExpressionParser_ParseLambda_NullPropagation_InstanceMethod_1 { // Arrange var customTypeProvider = new Mock(); - customTypeProvider.Setup(c => c.GetCustomTypes()).Returns(new HashSet { typeof(Foo) }); + customTypeProvider.Setup(c => c.GetCustomTypes()).Returns([typeof(Foo)]); var config = new ParsingConfig { CustomTypeProvider = customTypeProvider.Object @@ -1830,7 +1821,7 @@ public void DynamicExpressionParser_ParseLambda_NullPropagation_InstanceMethod_2 { // Arrange var customTypeProvider = new Mock(); - customTypeProvider.Setup(c => c.GetCustomTypes()).Returns(new HashSet { typeof(Foo) }); + customTypeProvider.Setup(c => c.GetCustomTypes()).Returns([typeof(Foo)]); var config = new ParsingConfig { CustomTypeProvider = customTypeProvider.Object @@ -1909,7 +1900,7 @@ public void DynamicExpressionParser_ParseLambda_String_TrimEnd_0_Parameters() var @delegate = expression.Compile(); - var result = (bool)@delegate.DynamicInvoke("This is a test "); + var result = (bool)@delegate.DynamicInvoke("This is a test ")!; // Assert result.Should().BeTrue(); @@ -1923,7 +1914,7 @@ public void DynamicExpressionParser_ParseLambda_String_TrimEnd_1_Parameter() var @delegate = expression.Compile(); - var result = (bool)@delegate.DynamicInvoke("This is a test..."); + var result = (bool)@delegate.DynamicInvoke("This is a test...")!; // Assert result.Should().BeTrue(); @@ -1947,7 +1938,7 @@ public void DynamicExpressionParser_ParseLambda_GenericExtensionMethod() var result = Enumerable.Where(testList, del); - var expected = testList.Where(x => new string[] { "User4", "User2" }.Contains(x.UserName)).ToList(); + var expected = testList.Where(x => new[] { "User4", "User2" }.Contains(x.UserName)).ToList(); // Assert Check.That(result).IsNotNull(); @@ -1959,7 +1950,8 @@ public void DynamicExpressionParser_ParseLambda_GenericExtensionMethod() public void DynamicExpressionParser_ParseLambda_Action() { // Arrange - var action = (Action)DynamicExpressionParser.ParseLambda(typeof(Action), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile(); + var action = (Action)DynamicExpressionParser.ParseLambda(typeof(Action), [Expression.Parameter(typeof(int), "x") + ], typeof(int), "x + 1").Compile(); // Act action(3); @@ -1971,10 +1963,9 @@ public void DynamicExpressionParser_ParseLambda_Func() // Arrange var func = (Func)DynamicExpressionParser.ParseLambda( typeof(Func), - new[] - { + [ Expression.Parameter(typeof(int), "x") - }, + ], typeof(int), "x + 1" ).Compile(); @@ -2006,7 +1997,7 @@ public void DynamicExpressionParser_ParseLambda_Func2(string? paramName, string var expression = DynamicExpressionParser.ParseLambda( delegateType, new ParsingConfig(), - new[] { valueParameter }, + [valueParameter], typeof(bool), test ); @@ -2037,7 +2028,7 @@ public void DynamicExpressionParser_ParseComparisonOperator_DynamicClass_For_Str var type = DynamicClassFactory.CreateType(props); - var dynamicClass = (DynamicClass)Activator.CreateInstance(type); + var dynamicClass = (DynamicClass)Activator.CreateInstance(type)!; dynamicClass.SetDynamicPropertyValue(field.Name, field.Value); // Act 1 @@ -2046,7 +2037,7 @@ public void DynamicExpressionParser_ParseComparisonOperator_DynamicClass_For_Str var @delegate = expression.Compile(); - var result = (bool)@delegate.DynamicInvoke(dynamicClass); + var result = (bool)@delegate.DynamicInvoke(dynamicClass)!; // Assert 1 result.Should().BeTrue(); @@ -2076,7 +2067,7 @@ public void DynamicExpressionParser_ParseLambda_HandleStringArray_For_StringJoin // Act var lambdaExpression = DynamicExpressionParser.ParseLambda(parameterExpressions.ToArray(), null, expression); var @delegate = lambdaExpression.Compile(); - var result = (string)@delegate.DynamicInvoke(new object[] { strArray }); + var result = (string)@delegate.DynamicInvoke([strArray])!; // Assert result.Should().Be("a,b,c"); @@ -2097,7 +2088,7 @@ public void DynamicExpressionParser_ParseLambda_HandleObjectArray_For_StringJoin // Act var lambdaExpression = DynamicExpressionParser.ParseLambda(parameterExpressions.ToArray(), null, expression); var @delegate = lambdaExpression.Compile(); - var result = (string)@delegate.DynamicInvoke(new object[] { objectArray }); + var result = (string?)@delegate.DynamicInvoke([objectArray]); // Assert result.Should().Be("1,2,3"); @@ -2118,7 +2109,7 @@ public void DynamicExpressionParser_ParseLambda_HandleIntArray_For_StringJoin() // Act var lambdaExpression = DynamicExpressionParser.ParseLambda(parameterExpressions.ToArray(), null, expression); var @delegate = lambdaExpression.Compile(); - var result = (string)@delegate.DynamicInvoke(intArray); + var result = (string?)@delegate.DynamicInvoke(intArray); // Assert result.Should().Be("1,2,3"); @@ -2137,6 +2128,7 @@ public DefaultDynamicLinqCustomTypeProviderForGenericExtensionMethod() : base(Pa { } - public override HashSet GetCustomTypes() => new HashSet(base.GetCustomTypes()) { typeof(Methods), typeof(MethodsItemExtension) }; + public override HashSet GetCustomTypes() => + [..base.GetCustomTypes(), typeof(Methods), typeof(MethodsItemExtension)]; } } \ No newline at end of file diff --git a/test/System.Linq.Dynamic.Core.Tests/EntitiesTests.Select.cs b/test/System.Linq.Dynamic.Core.Tests/EntitiesTests.Select.cs index df4cf56e..ab0b44c9 100644 --- a/test/System.Linq.Dynamic.Core.Tests/EntitiesTests.Select.cs +++ b/test/System.Linq.Dynamic.Core.Tests/EntitiesTests.Select.cs @@ -140,6 +140,6 @@ public void Entities_Select_DynamicClass_And_Call_Any() .Any("Name == \"Blog2\""); // Assert - Assert.Equal(true, result); + Assert.True(result); } } \ No newline at end of file