Skip to content

Commit f3c9147

Browse files
authored
Merge pull request #28 from I-RzR-I/feature/Remove_System_Linq_Dynamic_Core
Feature/remove system linq dynamic core
2 parents 9a0adf4 + 81302c7 commit f3c9147

34 files changed

Lines changed: 1838 additions & 44 deletions

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022-2024 RzR
3+
Copyright (c) 2022-2025 RzR
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![NuGet Version](https://img.shields.io/nuget/v/DomainCommonExtensions.svg?style=flat&logo=nuget)](https://www.nuget.org/packages/DomainCommonExtensions/)
44
[![Nuget Downloads](https://img.shields.io/nuget/dt/DomainCommonExtensions.svg?style=flat&logo=nuget)](https://www.nuget.org/packages/DomainCommonExtensions)
55

6-
This library/ repository was created as a way to simplify the development process. Here were written the usually used methods (extension methods) for some data types like `int, string, DateTime, Enum, bool, byte, Guid`, also there was added extensions for `List, Dictionary, DynamicList(using 'System.Linq.Dynamic.Core')` and other collections(`ICollection, IEnumerable, IList, HashSet, IQueryable`).
6+
This library/ repository was created as a way to simplify the development process. Here were written the usually used methods (extension methods) for some data types like `int, string, DateTime, Enum, bool, byte, Guid`, also there was added extensions for `List, Dictionary, DynamicList` and other collections(`ICollection, IEnumerable, IList, HashSet, IQueryable`).
77

88
In the repository was added an extension for `cryptography`, encrypting and decrypting string by key with RSA.
99

docs/CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,12 @@
115115

116116
### **v1.3.0.0**
117117
-> Fix test for `CalculateAge`;<br />
118-
-> Add new string extensions: `IsValidJson`, `IsValidJsonObject`, `IsValidJsonArray`.<br />
118+
-> Add new string extensions: `IsValidJson`, `IsValidJsonObject`, `IsValidJsonArray`;<br />
119+
120+
### **v2.0.0.0**
121+
-> Fix test for `CalculateAge`;<br />
122+
-> Add DateTime extension method `AsNotNull`;<br />
123+
-> Add new tests for `AsNotNull` methods;<br />
124+
-> Add `EnumerateUtils` enumerable utils some tests;<br />
125+
-> Adjust AES encryption(`AesEncryptString`, `AesDecryptString`) and expose iv as input;<br />
126+
-> Adjust dynamic property/ies select avoid `System.Linq.Dynamic.Core`;<br />

src/DomainCommonExtensions/ArraysExtensions/DynamicListExtensions.cs

Lines changed: 109 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818

1919
using System.Collections.Generic;
2020
using System.Linq;
21-
using System.Linq.Dynamic.Core;
2221
using System.Linq.Expressions;
2322
using DomainCommonExtensions.CommonExtensions;
23+
using DomainCommonExtensions.Helpers.Internal.AnonymousSelect;
24+
25+
#region OLD Using System.Linq.Dynamic.Core
26+
//using System.Linq.Dynamic.Core;
27+
//using System.Linq.Dynamic.Core.Parser;
28+
#endregion
2429

2530
#endregion
2631

@@ -32,8 +37,57 @@ namespace DomainCommonExtensions.ArraysExtensions
3237
/// <remarks></remarks>
3338
public static class DynamicListExtensions
3439
{
40+
#region OLD Using System.Linq.Dynamic.Core
41+
42+
/*
43+
// Using System.Linq.Dynamic.Core
44+
// // <summary>
45+
// // Parse input data (List) to dynamic result (list)
46+
// // </summary>
47+
// // <param name="input">List of T input data</param>
48+
// // <param name="fields">Required fields</param>
49+
// // <returns></returns>
50+
// // <typeparam name="T"></typeparam>
51+
// // <remarks></remarks>
52+
// public static IList<dynamic> ParseListOfTInDynamic<T>(this List<T> input, IEnumerable<string> fields = null)
53+
// {
54+
// var data = input.AsQueryable();
55+
// var requiredFields = typeof(T).GetSelectedFieldFromEntity(fields);
56+
//
57+
// var result = data.Select("new {" + requiredFields + "}").ToDynamicList();
58+
//
59+
// return result;
60+
// }
61+
62+
*/
63+
64+
/*
65+
// Using System.Linq.Dynamic.Core
66+
// /// <summary>
67+
// /// Parse input data (IEnumerable) to dynamic result (list)
68+
// /// </summary>
69+
// /// <param name="input">IEnumerable of T input data</param>
70+
// /// <param name="fields">Required fields</param>
71+
// /// <returns></returns>
72+
// /// <typeparam name="T"></typeparam>
73+
// /// <remarks></remarks>
74+
// public static IList<dynamic> ParseEnumerableOfTInDynamic<T>(this IEnumerable<T> input,
75+
// IEnumerable<string> fields = null)
76+
// {
77+
// var data = input.AsQueryable();
78+
// var requiredFields = typeof(T).GetSelectedFieldFromEntity(fields);
79+
80+
// var result = data.Select("new {" + requiredFields + "}").ToDynamicList();
81+
82+
// return result;
83+
// }
84+
85+
*/
86+
87+
#endregion
88+
3589
/// <summary>
36-
/// Parse input data (List) to dynamic result (list)
90+
/// Parse input data (IEnumerable) to dynamic result (list)
3791
/// </summary>
3892
/// <param name="input">List of T input data</param>
3993
/// <param name="fields">Required fields</param>
@@ -44,29 +98,45 @@ public static IList<dynamic> ParseListOfTInDynamic<T>(this List<T> input, IEnume
4498
{
4599
var data = input.AsQueryable();
46100
var requiredFields = typeof(T).GetSelectedFieldFromEntity(fields);
101+
var parameter = Expression.Parameter(data.ElementType, "x");
102+
var lambdaExpression = ExpressionHelper.ParseLambda(parameter, data.ElementType, false, requiredFields.Split(','));
103+
104+
var selectCall = Expression.Call(
105+
typeof(Queryable),
106+
"Select",
107+
new[] { parameter.Type, lambdaExpression.Body.Type/*source.ElementType*/ },
108+
data.Expression,
109+
Expression.Quote(lambdaExpression));
47110

48-
var result = data.Select("new {" + requiredFields + "}").ToDynamicList();
111+
var result = data.Provider.CreateQuery(selectCall);
49112

50-
return result;
113+
return result.Cast<dynamic>().ToList();
51114
}
52-
53115
/// <summary>
54-
/// Parse input data (IEnumerable) to dynamic result (list)
116+
/// Parse input data (List) to dynamic result (list)
55117
/// </summary>
56118
/// <param name="input">IEnumerable of T input data</param>
57119
/// <param name="fields">Required fields</param>
58120
/// <returns></returns>
59121
/// <typeparam name="T"></typeparam>
60122
/// <remarks></remarks>
61-
public static IList<dynamic> ParseEnumerableOfTInDynamic<T>(this IEnumerable<T> input,
62-
IEnumerable<string> fields = null)
123+
public static IList<dynamic> ParseEnumerableOfTInDynamic<T>(this IEnumerable<T> input, IEnumerable<string> fields = null)
63124
{
64125
var data = input.AsQueryable();
65126
var requiredFields = typeof(T).GetSelectedFieldFromEntity(fields);
127+
var parameter = Expression.Parameter(data.ElementType, "x");
128+
var lambdaExpression = ExpressionHelper.ParseLambda(parameter, data.ElementType, false, requiredFields.Split(','));
66129

67-
var result = data.Select("new {" + requiredFields + "}").ToDynamicList();
130+
var selectCall = Expression.Call(
131+
typeof(Queryable),
132+
"Select",
133+
new[] { parameter.Type, lambdaExpression.Body.Type/*source.ElementType*/ },
134+
data.Expression,
135+
Expression.Quote(lambdaExpression));
136+
137+
var result = data.Provider.CreateQuery(selectCall);
68138

69-
return result;
139+
return result.Cast<dynamic>().ToList();
70140
}
71141

72142
/// <summary>
@@ -79,12 +149,36 @@ public static IList<dynamic> ParseEnumerableOfTInDynamic<T>(this IEnumerable<T>
79149
public static IQueryable SelectProperty(this IQueryable source, string prop)
80150
{
81151
var parameter = Expression.Parameter(source.ElementType, "x");
82-
var property = prop.Split(',')
83-
.Aggregate((Expression)parameter, Expression.Property);
84-
var selector = Expression.Lambda(property, parameter);
152+
var propEx = Expression.Property(parameter, prop);
153+
var selector = Expression.Lambda(propEx, parameter);
154+
var selectCall = Expression.Call(
155+
typeof(Queryable),
156+
"Select",
157+
new[] { parameter.Type, propEx.Type },
158+
source.Expression,
159+
Expression.Quote(selector));
160+
161+
return source.Provider.CreateQuery(selectCall);
162+
}
163+
164+
/// <summary>
165+
/// Generate select multiple properties
166+
/// </summary>
167+
/// <param name="source">Data source IQueryable</param>
168+
/// <param name="props">Properties name</param>
169+
/// <returns></returns>
170+
/// <remarks></remarks>
171+
public static IQueryable SelectMultipleProperties(this IQueryable source, params string[] props)
172+
{
173+
var parameter = Expression.Parameter(source.ElementType, "x");
174+
var lambdaExpression = ExpressionHelper.ParseLambda(parameter, source.ElementType, true, props);
175+
85176
var selectCall = Expression.Call(
86-
typeof(Queryable), "Select", new[] { parameter.Type, property.Type },
87-
source.Expression, Expression.Quote(selector));
177+
typeof(Queryable),
178+
"Select",
179+
new[] { parameter.Type, lambdaExpression.Body.Type/*source.ElementType*/ },
180+
source.Expression,
181+
Expression.Quote(lambdaExpression));
88182

89183
return source.Provider.CreateQuery(selectCall);
90184
}

src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
using DomainCommonExtensions.CommonExtensions.TypeParam;
2727
using DomainCommonExtensions.DataTypeExtensions;
2828

29+
// ReSharper disable UnusedParameter.Local
30+
// ReSharper restore PossibleMultipleEnumeration
31+
2932
namespace DomainCommonExtensions.ArraysExtensions
3033
{
3134
/// <summary>
@@ -366,7 +369,6 @@ public static string ListToString<T>(this IEnumerable<T> list, string delimiter)
366369
}
367370

368371
return result.ToString();
369-
// ReSharper restore PossibleMultipleEnumeration
370372
}
371373

372374
/// <summary>

src/DomainCommonExtensions/CommonExtensions/Encryption/AESEncryptionExtensions.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,21 @@ public static partial class CryptoExtensions
3535
/// </summary>
3636
/// <param name="plainText">Plaint text to be encrypted.</param>
3737
/// <param name="key">Encryption key.</param>
38+
/// <param name="iv">Initialization vector.</param>
3839
/// <returns></returns>
39-
public static string AesEncryptString(this string plainText, string key)
40+
public static string AesEncryptString(this string plainText, string key, byte[] iv)
4041
{
4142
key.ThrowIfArgNullOrEmpty(nameof(key));
4243
plainText.ThrowIfArgNull(nameof(plainText));
44+
iv.ThrowIfArgNull(nameof(iv));
4345

44-
var iv = new byte[16];
46+
//var iv = new byte[16];
4547
byte[] array;
46-
4748
using (var aes = Aes.Create())
4849
{
4950
aes.Key = Encoding.UTF8.GetBytes(key);
5051
aes.IV = iv;
51-
5252
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
53-
5453
using (var memoryStream = new MemoryStream())
5554
{
5655
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
@@ -73,21 +72,21 @@ public static string AesEncryptString(this string plainText, string key)
7372
/// </summary>
7473
/// <param name="cipherText">Encrypted text to be decrypted</param>
7574
/// <param name="key">Decryption key.</param>
75+
/// <param name="iv">Initialization vector.</param>
7676
/// <returns></returns>
77-
public static string AesDecryptString(this string cipherText, string key)
77+
public static string AesDecryptString(this string cipherText, string key, byte[] iv)
7878
{
7979
key.ThrowIfArgNullOrEmpty(nameof(key));
8080
cipherText.ThrowIfArgNullOrEmpty(nameof(cipherText));
81+
iv.ThrowIfArgNull(nameof(iv));
8182

82-
var iv = new byte[16];
83+
//var iv = new byte[16];
8384
var buffer = Convert.FromBase64String(cipherText);
84-
8585
using (var aes = Aes.Create())
8686
{
8787
aes.Key = Encoding.UTF8.GetBytes(key);
8888
aes.IV = iv;
8989
var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
90-
9190
using (var memoryStream = new MemoryStream(buffer))
9291
{
9392
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))

src/DomainCommonExtensions/CommonExtensions/ReflectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void CopyProperties(this object source, object destination)
5252
var targetProperty = typeDest.GetProperty(srcProp.Name);
5353
if (targetProperty.IsNull())
5454
continue;
55-
if (!targetProperty.CanWrite)
55+
if (!targetProperty!.CanWrite)
5656
continue;
5757
if (targetProperty.GetSetMethod(true) != null && targetProperty.GetSetMethod(true).IsPrivate)
5858
continue;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// ***********************************************************************
2+
// Assembly : RzR.Shared.Extensions.DomainCommonExtensions
3+
// Author : RzR
4+
// Created On : 2025-01-08 09:27
5+
//
6+
// Last Modified By : RzR
7+
// Last Modified On : 2025-01-08 09:28
8+
// ***********************************************************************
9+
// <copyright file="TypeBuilderExtensions.cs" company="RzR SOFT & TECH">
10+
// Copyright © RzR. All rights reserved.
11+
// </copyright>
12+
//
13+
// <summary>
14+
// </summary>
15+
// ***********************************************************************
16+
17+
#region U S A G E S
18+
19+
// ReSharper disable RedundantUsingDirective
20+
21+
using System;
22+
using System.Reflection;
23+
using System.Reflection.Emit;
24+
25+
#endregion
26+
27+
namespace DomainCommonExtensions.CommonExtensions
28+
{
29+
/// -------------------------------------------------------------------------------------------------
30+
/// <summary>
31+
/// A type builder extensions.
32+
/// </summary>
33+
/// =================================================================================================
34+
public static class TypeBuilderExtensions
35+
{
36+
37+
#if !(NET35 || NET40 || SILVERLIGHT || WPSL || UAP10_0)
38+
39+
/// -------------------------------------------------------------------------------------------------
40+
/// <summary>
41+
/// Creates a System.Type object for the class. After defining fields and methods
42+
/// on the class, CreateType is called in order to load its Type object.
43+
/// </summary>
44+
/// <param name="tb">The TypeBuilder to act on.</param>
45+
/// <returns>
46+
/// Returns the new System.Type object for this class.
47+
/// </returns>
48+
/// =================================================================================================
49+
public static Type CreateType(this TypeBuilder tb)
50+
{
51+
return tb.CreateTypeInfo().AsType();
52+
}
53+
#endif
54+
55+
#if NET35 || NET40 || SILVERLIGHT || WPSL || NETCOREAPP || NETSTANDARD2_1
56+
57+
/// -------------------------------------------------------------------------------------------------
58+
/// <summary>
59+
/// A TypeBuilder extension method that define property.
60+
/// </summary>
61+
/// <param name="tb">The TypeBuilder to act on.</param>
62+
/// <param name="name">The name.</param>
63+
/// <param name="attributes">The attributes.</param>
64+
/// <param name="callingConvention">The calling convention.</param>
65+
/// <param name="returnType">Type of the return.</param>
66+
/// <param name="parameterTypes">List of types of the parameters.</param>
67+
/// <returns>
68+
/// A PropertyBuilder.
69+
/// </returns>
70+
/// =================================================================================================
71+
public static PropertyBuilder DefineProperty(this TypeBuilder tb, string name, PropertyAttributes attributes,
72+
CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
73+
{
74+
return tb.DefineProperty(name, attributes, returnType, parameterTypes);
75+
}
76+
77+
/// -------------------------------------------------------------------------------------------------
78+
/// <summary>
79+
/// A TypeBuilder extension method that converts a builder to a type.
80+
/// </summary>
81+
/// <param name="builder">The builder to act on.</param>
82+
/// <returns>
83+
/// A Type.
84+
/// </returns>
85+
/// =================================================================================================
86+
public static Type AsType(this TypeBuilder builder)
87+
{
88+
return builder;
89+
}
90+
91+
/// -------------------------------------------------------------------------------------------------
92+
/// <summary>
93+
/// A GenericTypeParameterBuilder extension method that converts a builder to a type.
94+
/// </summary>
95+
/// <param name="builder">The builder to act on.</param>
96+
/// <returns>
97+
/// A Type.
98+
/// </returns>
99+
/// =================================================================================================
100+
public static Type AsType(this GenericTypeParameterBuilder builder)
101+
{
102+
return builder;
103+
}
104+
#endif
105+
}
106+
}

0 commit comments

Comments
 (0)