Skip to content

Commit

Permalink
DynamicClassFactory.CreateInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 10, 2024
1 parent 82004ff commit b0b7fd5
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq.Dynamic.Core.NewtonsoftJson.Models;
using System.Reflection;
using JsonConverter.Abstractions.Models;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -35,11 +34,6 @@ private class JTokenResolvers : Dictionary<JTokenType, Func<JToken, DynamicJsonC
{ JTokenType.Uri, (o, _) => o.Value<Uri>() },
};

//internal static object? ToDynamicClass(this JValue src)
//{
// return src.Value;
//}

internal static DynamicClass? ToDynamicClass(this JObject? src, DynamicJsonClassOptions? options = null)
{
if (src == null)
Expand All @@ -58,7 +52,7 @@ private class JTokenResolvers : Dictionary<JTokenType, Func<JToken, DynamicJsonC
}
}

return CreateInstance(dynamicPropertiesWithValue);
return DynamicClassFactory.CreateInstance(dynamicPropertiesWithValue);
}

internal static IEnumerable ToDynamicJsonClassArray(this JArray? src, DynamicJsonClassOptions? options = null)
Expand Down Expand Up @@ -192,16 +186,4 @@ private static T[] ConvertToTypedArrayGeneric<T>(IEnumerable<object> src)
{
return src.Cast<T>().ToArray();
}

private static DynamicClass CreateInstance(IList<DynamicPropertyWithValue> dynamicPropertiesWithValue)
{
var type = DynamicClassFactory.CreateType(dynamicPropertiesWithValue.Cast<DynamicProperty>().ToArray());
var dynamicClass = (DynamicClass)Activator.CreateInstance(type)!;
foreach (var dynamicPropertyWithValue in dynamicPropertiesWithValue.Where(p => p.Value != null))
{
dynamicClass.SetDynamicPropertyValue(dynamicPropertyWithValue.Name, dynamicPropertyWithValue.Value!);
}

return dynamicClass;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ public static JToken Single(this JArray source, LambdaExpression lambda)
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>An <see cref="JArray"/> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
public static JArray SkipWhile(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object[]? args)
public static JArray SkipWhile(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
Expand All @@ -837,7 +837,7 @@ public static JArray SkipWhile(this JArray source, NewtonsoftJsonParsingConfig c
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>An <see cref="JArray"/> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
public static JArray SkipWhile(this JArray source, string predicate, params object[]? args)
public static JArray SkipWhile(this JArray source, string predicate, params object?[] args)
{
return SkipWhile(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
Expand Down Expand Up @@ -887,7 +887,7 @@ public static JArray ThenBy(this JArray source, NewtonsoftJsonParsingConfig conf
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source, config).OrderBy("0"); // Workaround to get IOrderedQueryable
return ToJArray(() => queryable.ThenBy(ordering, args));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq.Dynamic.Core.SystemTextJson.Models;
using System.Reflection;
using System.Text.Json;
using JsonConverter.Abstractions.Models;
Expand Down Expand Up @@ -43,7 +42,7 @@ private class JTokenResolvers : Dictionary<JsonValueKind, Func<JsonElement, Dyna
}
}

return CreateInstance(dynamicPropertiesWithValue);
return DynamicClassFactory.CreateInstance(dynamicPropertiesWithValue);
}

public static IEnumerable ToDynamicJsonClassArray(this JsonElement? src, DynamicJsonClassOptions? options = null)
Expand Down Expand Up @@ -141,16 +140,4 @@ private static T[] ConvertToTypedArrayGeneric<T>(IEnumerable<object> src)
{
return src.Cast<T>().ToArray();
}

private static DynamicClass CreateInstance(IList<DynamicPropertyWithValue> dynamicPropertiesWithValue)
{
var type = DynamicClassFactory.CreateType(dynamicPropertiesWithValue.Cast<DynamicProperty>().ToArray());
var dynamicClass = (DynamicClass)Activator.CreateInstance(type)!;
foreach (var dynamicPropertyWithValue in dynamicPropertiesWithValue.Where(p => p.Value != null))
{
dynamicClass.SetDynamicPropertyValue(dynamicPropertyWithValue.Name, dynamicPropertyWithValue.Value!);
}

return dynamicClass;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ public static JsonDocument Skip(this JsonDocument source, int count)
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>An <see cref="JsonDocument"/> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
public static JsonDocument SkipWhile(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object[]? args)
public static JsonDocument SkipWhile(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
Expand All @@ -1012,7 +1012,7 @@ public static JsonDocument SkipWhile(this JsonDocument source, SystemTextJsonPar
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>An <see cref="JsonDocument"/> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
public static JsonDocument SkipWhile(this JsonDocument source, string predicate, params object[]? args)
public static JsonDocument SkipWhile(this JsonDocument source, string predicate, params object?[] args)
{
return SkipWhile(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
Expand Down
26 changes: 24 additions & 2 deletions src/System.Linq.Dynamic.Core/DynamicClassFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using JetBrains.Annotations;

#if WINDOWS_APP
using System.Linq;
#endif
Expand Down Expand Up @@ -149,6 +149,28 @@ public static Type CreateGenericComparerType(Type comparerGenericType, Type comp
return type;
}

/// <summary>
/// Create a new instance of a dynamic class with a given set of public properties (with the value).
/// In case the value is null, the property is not set.
/// </summary>
/// <param name="dynamicPropertiesWithValue">A list of DynamicProperties with a value</param>
/// <param name="createParameterCtor">Create a constructor with parameters. Default set to true. Note that for Linq-to-Database objects, this needs to be set to false.</param>
/// <returns>Instance of <see cref="DynamicClass"/></returns>
public static DynamicClass CreateInstance(IList<DynamicPropertyWithValue> dynamicPropertiesWithValue, bool createParameterCtor = true)
{
Check.HasNoNulls(dynamicPropertiesWithValue);

var type = CreateType(dynamicPropertiesWithValue.Cast<DynamicProperty>().ToArray(), createParameterCtor);
var dynamicClass = (DynamicClass)Activator.CreateInstance(type)!;

foreach (var dynamicPropertyWithValue in dynamicPropertiesWithValue.Where(p => p.Value != null))
{
dynamicClass.SetDynamicPropertyValue(dynamicPropertyWithValue.Name, dynamicPropertyWithValue.Value!);
}

return dynamicClass;
}

/// <summary>
/// The CreateType method creates a new data class with a given set of public properties and returns the System.Type object for the newly created class. If a data class with an identical sequence of properties has already been created, the System.Type object for this class is returned.
/// Data classes implement private instance variables and read/write property accessors for the specified properties.Data classes also override the Equals and GetHashCode members to implement by-value equality.
Expand All @@ -172,7 +194,7 @@ public static Type CreateGenericComparerType(Type comparerGenericType, Type comp
/// </example>
public static Type CreateType(IList<DynamicProperty> properties, bool createParameterCtor = true)
{
Check.HasNoNulls(properties, nameof(properties));
Check.HasNoNulls(properties);

Type[] types = properties.Select(p => p.Type).ToArray();
string[] names = properties.Select(p => p.Name).ToArray();
Expand Down
55 changes: 27 additions & 28 deletions src/System.Linq.Dynamic.Core/DynamicProperty.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
namespace System.Linq.Dynamic.Core
namespace System.Linq.Dynamic.Core;

/// <summary>
/// DynamicProperty
/// </summary>
public class DynamicProperty
{
/// <summary>
/// DynamicProperty
/// Initializes a new instance of the <see cref="DynamicProperty"/> class.
/// </summary>
public class DynamicProperty
/// <param name="name">The name from the property.</param>
/// <param name="type">The type from the property.</param>
public DynamicProperty(string name, Type type)
{
/// <summary>
/// Initializes a new instance of the <see cref="DynamicProperty"/> class.
/// </summary>
/// <param name="name">The name from the property.</param>
/// <param name="type">The type from the property.</param>
public DynamicProperty(string name, Type type)
{
Name = name;
Type = type;
}
Name = name;
Type = type;
}

/// <summary>
/// Gets the name from the property.
/// </summary>
/// <value>
/// The name from the property.
/// </value>
public string Name { get; }
/// <summary>
/// Gets the name from the property.
/// </summary>
/// <value>
/// The name from the property.
/// </value>
public string Name { get; }

/// <summary>
/// Gets the type from the property.
/// </summary>
/// <value>
/// The type from the property.
/// </value>
public Type Type { get; }
}
/// <summary>
/// Gets the type from the property.
/// </summary>
/// <value>
/// The type from the property.
/// </value>
public Type Type { get; }
}
22 changes: 22 additions & 0 deletions src/System.Linq.Dynamic.Core/DynamicPropertyWithValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace System.Linq.Dynamic.Core;

/// <summary>
/// DynamicPropertyWithValue
/// </summary>
public class DynamicPropertyWithValue : DynamicProperty
{
/// <summary>
/// Gets the value from the property.
/// </summary>
public object? Value { get; }

/// <summary>
/// Initializes a new instance of the <see cref="DynamicPropertyWithValue"/> class.
/// </summary>
/// <param name="name">The name from the property.</param>
/// <param name="value">The value from the property.</param>
public DynamicPropertyWithValue(string name, object? value) : base(name, value?.GetType() ?? typeof(object))
{
Value = value;
}
}
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 @@ -2314,7 +2314,7 @@ public static IQueryable SkipWhile(this IQueryable source, ParsingConfig config,
return CreateQuery(_skipWhilePredicate, source, lambda);
}

/// <inheritdoc cref="SkipWhile(IQueryable, ParsingConfig, string, object[])"/>
/// <inheritdoc cref="SkipWhile(IQueryable, ParsingConfig, string, object?[])"/>
public static IQueryable SkipWhile(this IQueryable source, string predicate, params object?[] args)
{
return SkipWhile(source, ParsingConfig.Default, predicate, args);
Expand Down

0 comments on commit b0b7fd5

Please sign in to comment.