Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ public static IQueryable GroupJoin([NotNull] this IQueryable outer, [CanBeNull]
typeof(Queryable), nameof(Queryable.GroupJoin),
new[] { outer.ElementType, innerType, outerSelectorLambda.Body.Type, resultSelectorLambda.Body.Type },
outer.Expression,
Expression.Constant(inner),
inner.AsQueryable().Expression,
Expression.Quote(outerSelectorLambda),
Expression.Quote(innerSelectorLambda),
Expression.Quote(resultSelectorLambda)));
Expand Down
4 changes: 4 additions & 0 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,10 @@ Type FindType(string name)
{
return _root.Type;
}
if (this._parsingConfig.AllowNewToEvaluateAnyType && Type.GetType(name) != null)
{
return Type.GetType(name);
}

return null;
}
Expand Down
51 changes: 51 additions & 0 deletions src/System.Linq.Dynamic.Core/ParsingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Reflection;

namespace System.Linq.Dynamic.Core
{
Expand All @@ -7,6 +8,26 @@ namespace System.Linq.Dynamic.Core
/// </summary>
public class ParsingConfig
{
/// <summary>
/// Get an instance of ParsingConfig
/// </summary>
public ParsingConfig()
{
#if !(DOTNET5_1 || WINDOWS_APP || UAP10_0 || NETSTANDARD)
AppDomain.CurrentDomain.TypeResolve += this.CurrentDomain_TypeResolve;
#endif
}

/// <summary>
/// Cleanup
/// </summary>
~ParsingConfig()
{
#if !(DOTNET5_1 || WINDOWS_APP || UAP10_0 || NETSTANDARD)
AppDomain.CurrentDomain.TypeResolve -= this.CurrentDomain_TypeResolve;
#endif
}

/// <summary>
/// Default ParsingConfig
/// </summary>
Expand Down Expand Up @@ -62,5 +83,35 @@ public IDynamicLinkCustomTypeProvider CustomTypeProvider
/// See https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.1#linq-groupby-translation
/// </summary>
public bool EvaluateGroupByAtDatabase { get; set; }

/// <summary>
/// Allows the New() keyword to evaluate any available Type
/// </summary>
public bool AllowNewToEvaluateAnyType { get; set; } = false;

#if !(DOTNET5_1 || WINDOWS_APP || UAP10_0 || NETSTANDARD)
/// <summary>
/// Custom type resolver to allow ExpressionParser to find any type
/// registered in the current application domain. You can add additional
/// type resolvers in your application.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <returns></returns>
protected Assembly CurrentDomain_TypeResolve(object sender, ResolveEventArgs args)
{
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
Type t = assembly.GetType(args.Name, false, true);

if (t != null)
{
return assembly;
}
}

return null;
}
#endif
}
}
51 changes: 51 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/QueryableTests.Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ public class Example
public DayOfWeek DOW { get; set; }
public int Sec { get; set; }
public int? SecNull { get; set; }

public class NestedDto
{
public string Name { get; set; }

public class NestedDto2
{
public string Name2 { get; set; }
}
}
}

public class ExampleWithConstructor
Expand Down Expand Up @@ -227,6 +237,47 @@ public void Select_Dynamic_IntoTypeWithNullableProperties2()
Check.That(resultDynamic.Last()).Equals(result.Last());
}

[Fact]
public void Select_Dynamic_IntoKnownNestedType()
{

#if (DOTNET5_1 || WINDOWS_APP || UAP10_0 || NETSTANDARD)
return;
#endif
var config = new ParsingConfig();
config.AllowNewToEvaluateAnyType = true;

// Act
IQueryable<string> data = new List<string>() { "name1", "name2" }.AsQueryable();
IQueryable<Example.NestedDto> projectedData =
(IQueryable<Example.NestedDto>) data.Select(config, $"new {typeof(Example.NestedDto).FullName}(~ as Name)");

// Assert
Check.That(projectedData.First().Name).Equals("name1");
Check.That(projectedData.Last().Name).Equals("name2");
}

[Fact]
public void Select_Dynamic_IntoKnownNestedTypeSecondLevel()
{

#if (DOTNET5_1 || WINDOWS_APP || UAP10_0 || NETSTANDARD)
return;
#endif

var config = new ParsingConfig();
config.AllowNewToEvaluateAnyType = true;

// Act
IQueryable<string> data = new List<string>() { "name1", "name2" }.AsQueryable();
IQueryable<Example.NestedDto.NestedDto2> projectedData =
(IQueryable<Example.NestedDto.NestedDto2>)data.Select(config, $"new {typeof(Example.NestedDto.NestedDto2).FullName}(~ as Name2)");

// Assert
Check.That(projectedData.First().Name2).Equals("name1");
Check.That(projectedData.Last().Name2).Equals("name2");
}

[Fact]
public void Select_Dynamic_Exceptions()
{
Expand Down