Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;

namespace System.Linq.Dynamic.Core.Parser;

internal static class EnumerationsAndWellKnownTypesFromMscorlib
{
private readonly static string SystemPrivateCoreLib = typeof(StringComparer).GetTypeInfo().Assembly.FullName!;
private readonly static string SystemPrivateUri = typeof(UriFormat).GetTypeInfo().Assembly.FullName!;
private readonly static string SystemPrivateXml = typeof(XmlNodeType).GetTypeInfo().Assembly.FullName!;
private readonly static string SystemPrivateXmlLinq = typeof(XObject).GetTypeInfo().Assembly.FullName!;

/// <summary>
/// Enum types and well-known types.
/// </summary>
public static readonly ConcurrentDictionary<string, Type> PredefinedEnumerationTypes = new(StringComparer.OrdinalIgnoreCase);

static EnumerationsAndWellKnownTypesFromMscorlib()
{
var list = AddEnumsAndWellKnownTypesFromAssembly(SystemPrivateUri);
list.AddRange(AddEnumsAndWellKnownTypesFromAssembly(SystemPrivateCoreLib));
list.AddRange(AddEnumsAndWellKnownTypesFromAssembly(SystemPrivateXml));
list.AddRange(AddEnumsAndWellKnownTypesFromAssembly(SystemPrivateXmlLinq));

#if !(NET35 || NETSTANDARD1_3)
var systemPrivateDataContractSerialization = typeof(Runtime.Serialization.DataContractResolver).GetTypeInfo().Assembly.FullName!;
list.AddRange(AddEnumsAndWellKnownTypesFromAssembly(systemPrivateDataContractSerialization));
#endif
foreach (var group in list.GroupBy(t => t.Name))
{
Add(group);
}
}

private static List<Type> AddEnumsAndWellKnownTypesFromAssembly(string assemblyName)
{
try
{
var assembly = Assembly.Load(new AssemblyName(assemblyName));
var types = assembly.GetTypes().ToArray();

var enumTypes = types.Where(t => t.GetTypeInfo().IsEnum && t.GetTypeInfo().IsPublic);
var enumLikeTypes = FindEnumLikeTypes(types.Where(x => x == typeof(StringComparer)).ToArray());

return enumTypes.Union(enumLikeTypes).ToList();
}
catch
{
return [];
}
}

private static Type[] FindEnumLikeTypes(Type[] types)
{
try
{
return types
.Where(t => t.GetTypeInfo().IsPublic && !t.GetTypeInfo().IsEnum && HasStaticPropertiesOrFieldsOfOwnType(t))
.ToArray();
}
catch
{
return [];
}
}

private static bool HasStaticPropertiesOrFieldsOfOwnType(Type type)
{
var baseType = type.GetTypeInfo().BaseType;

var anyStaticProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Static)
.Any(p => p.PropertyType == type || p.PropertyType == baseType);

if (anyStaticProperties)
{
return true;
}

var anyStaticFields = type.GetFields(BindingFlags.Public | BindingFlags.Static)
.Any(f => f.FieldType == type || f.FieldType == baseType);

return anyStaticFields;
}

private static void Add(IGrouping<string, Type> group)
{
if (group.Count() == 1)
{
var singleType = group.Single();
PredefinedEnumerationTypes.TryAdd(group.Key, singleType);
PredefinedEnumerationTypes.TryAdd(singleType.FullName!, singleType);
}
else
{
foreach (var fullType in group)
{
PredefinedEnumerationTypes.TryAdd(fullType.FullName!, fullType);
}
}
}
}
59 changes: 0 additions & 59 deletions src/System.Linq.Dynamic.Core/Parser/EnumerationsFromMscorlib.cs

This file was deleted.

11 changes: 10 additions & 1 deletion src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,7 @@
{
if (!propertyNames.Add(propName!))
{
throw ParseError(exprPos, Res.DuplicateIdentifier, propName);

Check warning on line 1517 in src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

View workflow job for this annotation

GitHub Actions / Windows: Build and Tests

Possible null reference argument for parameter 'args' in 'Exception ExpressionParser.ParseError(int pos, string format, params object[] args)'.

Check warning on line 1517 in src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

View workflow job for this annotation

GitHub Actions / Linux: Build and Tests

Possible null reference argument for parameter 'args' in 'Exception ExpressionParser.ParseError(int pos, string format, params object[] args)'.
}

properties.Add(new DynamicProperty(propName!, expr.Type));
Expand Down Expand Up @@ -2317,7 +2317,16 @@
{
if (new[] { "Concat", "Contains", "ContainsKey", "DefaultIfEmpty", "Except", "Intersect", "Skip", "Take", "Union", "SequenceEqual" }.Contains(methodName))
{
args = [instance, args[0]];
if (args.Length == 1)
{
args = [instance, args[0]];
}
else
{
var argsAsList = new List<Expression> { instance };
argsAsList.AddRange(args);
args = argsAsList.ToArray();
}
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public bool TryGetValue(string text, out AnyOf<string, Expression, Type> value)
}

// 4. Try to get as an enum from the system namespace
if (_config.SupportEnumerationsFromSystemNamespace && EnumerationsFromMscorlib.PredefinedEnumerationTypes.TryGetValue(text, out var predefinedEnumType))
if (_config.SupportEnumerationsFromSystemNamespace && EnumerationsAndWellKnownTypesFromMscorlib.PredefinedEnumerationTypes.TryGetValue(text, out var predefinedEnumType))
{
value = predefinedEnumType;
return true;
Expand Down
32 changes: 32 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Tests.Helpers;
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
using System.Net.WebSockets;
using System.Xml;
using FluentAssertions;
using Moq;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -853,6 +855,36 @@ public void ExpressionTests_Enum()
Check.That(resultEqualStringMixedCaseParamRight.Single()).Equals(TestEnum.Var5);
}

[Fact]
public void ExpressionTests_Enum_XmlNodeType()
{
// Arrange
var lst = new[] { XmlNodeType.Text, XmlNodeType.Element };
var qry = lst.AsQueryable();

// Act
var result = lst.Count(it => new[] { XmlNodeType.Text }.Contains(it));
var dynamicResult = qry.Count("new XmlNodeType[] { XmlNodeType.Text }.Contains(it)");

// Assert
dynamicResult.Should().Be(result);
}

[Fact]
public void ExpressionTests_WellKnownTypes_StringComparer()
{
// Arrange
var lst = new[] { "test" };
var qry = lst.AsQueryable();

// Act
var result = lst.Count(it => new string[] { "Test" }.Contains(it, StringComparer.OrdinalIgnoreCase));
var dynamicResult = qry.Count("new string[] { \"Test\" }.Contains(it, StringComparer.OrdinalIgnoreCase)");

// Assert
dynamicResult.Should().Be(result);
}

[Fact]
public void ExpressionTests_Enum_Property_Equality_Using_Argument()
{
Expand Down
Loading