Skip to content

Commit

Permalink
Merge pull request #4 from hjkl950217/move-Nova
Browse files Browse the repository at this point in the history
合并nova
  • Loading branch information
hjkl950217 authored Oct 29, 2020
2 parents a89b748 + c036e67 commit 5faf373
Show file tree
Hide file tree
Showing 64 changed files with 2,693 additions and 66 deletions.
14 changes: 14 additions & 0 deletions CkTools/CkTools.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CkTools.Abstraction.Test",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CKTools.BaseExtensions.Test", "Test\CKTools.BaseExtensions.Test\CKTools.BaseExtensions.Test.csproj", "{2C2E3545-4B13-4746-A702-0F999240818F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CkTools.Nova", "Src\CkTools.Nova\CkTools.Nova.csproj", "{6713F0C2-BB89-463B-AEB6-00BBFEC6DE00}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CkTools.Nova.Test", "Test\CkTools.Nova.Test\CkTools.Nova.Test.csproj", "{5837DBA3-47A0-43BF-9346-F2C8878F6AC4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -69,6 +73,14 @@ Global
{2C2E3545-4B13-4746-A702-0F999240818F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C2E3545-4B13-4746-A702-0F999240818F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C2E3545-4B13-4746-A702-0F999240818F}.Release|Any CPU.Build.0 = Release|Any CPU
{6713F0C2-BB89-463B-AEB6-00BBFEC6DE00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6713F0C2-BB89-463B-AEB6-00BBFEC6DE00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6713F0C2-BB89-463B-AEB6-00BBFEC6DE00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6713F0C2-BB89-463B-AEB6-00BBFEC6DE00}.Release|Any CPU.Build.0 = Release|Any CPU
{5837DBA3-47A0-43BF-9346-F2C8878F6AC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5837DBA3-47A0-43BF-9346-F2C8878F6AC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5837DBA3-47A0-43BF-9346-F2C8878F6AC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5837DBA3-47A0-43BF-9346-F2C8878F6AC4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -82,6 +94,8 @@ Global
{759F7D6B-B88F-4B90-8DA6-9F6E8DD61DEC} = {D80CD124-05E6-4F2F-ADCD-328ACACA04E5}
{B9045104-1DE3-40C9-9DF2-7E896AEFE751} = {D80CD124-05E6-4F2F-ADCD-328ACACA04E5}
{2C2E3545-4B13-4746-A702-0F999240818F} = {D80CD124-05E6-4F2F-ADCD-328ACACA04E5}
{6713F0C2-BB89-463B-AEB6-00BBFEC6DE00} = {D3146C29-9318-45EC-B9BA-1EC7653421FB}
{5837DBA3-47A0-43BF-9346-F2C8878F6AC4} = {D80CD124-05E6-4F2F-ADCD-328ACACA04E5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AB04A57E-6447-4E76-862D-5D1B1638F2B6}
Expand Down
26 changes: 20 additions & 6 deletions CkTools/Src/CkTools.Abstraction/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace System
{
Expand All @@ -14,7 +15,8 @@ public static class ObjectExtensions
/// <param name="obj"> 要检查的对象</param>
/// <param name="paramName">抛出异常时,显示的参数名</param>
/// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null时抛出</exception>
public static void CheckNullWithException<T>(this T obj, string paramName)
public static void CheckNullWithException<T>(this T? obj, string paramName)
where T : class
{
if (obj == null) throw new ArgumentNullException(paramName);
}
Expand All @@ -27,7 +29,8 @@ public static void CheckNullWithException<T>(this T obj, string paramName)
/// <param name="paramName">抛出异常时,显示的参数名</param>
/// <param name="message"> 抛出异常时,显示的错误信息</param>
/// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null时抛出</exception>
public static void CheckNullWithException<T>(this T obj, string paramName, string message)
public static void CheckNullWithException<T>(this T? obj, string paramName, string message)
where T : class
{
if (obj == null) throw new ArgumentNullException(paramName, message);
}
Expand All @@ -38,7 +41,7 @@ public static void CheckNullWithException<T>(this T obj, string paramName, strin
/// <param name="obj"> 要检查的对象</param>
/// <param name="paramName">抛出异常时,显示的参数名</param>
/// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null或emtpy时抛出</exception>
public static void CheckNullOrEmptyWithException(this IEnumerable obj, string paramName)
public static void CheckNullOrEmptyWithException(this IEnumerable? obj, string paramName)
{
if (obj.IsNullOrEmpty()) throw new ArgumentNullException(paramName);
}
Expand All @@ -50,7 +53,7 @@ public static void CheckNullOrEmptyWithException(this IEnumerable obj, string pa
/// <param name="paramName">抛出异常时,显示的参数名</param>
/// <param name="message"> 抛出异常时,显示的错误信息</param>
/// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null或emtpy时抛出</exception>
public static void CheckNullOrEmptyWithException(this IEnumerable obj, string paramName, string message)
public static void CheckNullOrEmptyWithException(this IEnumerable? obj, string paramName, string message)
{
if (obj.IsNullOrEmpty()) throw new ArgumentNullException(paramName, message);
}
Expand Down Expand Up @@ -108,7 +111,7 @@ public static bool IsNull(this object value)
/// <typeparam name="T">对象类型</typeparam>
/// <param name="value">要判断的数组</param>
/// <returns>判断结果,null或空数组返回true,否则返回false</returns>
public static bool IsNullOrEmpty<T>(this T[] value)
public static bool IsNullOrEmpty<T>(this T[]? value)
{
return value == null || value.Length == 0;
}
Expand Down Expand Up @@ -140,7 +143,7 @@ public static bool IsNullOrEmpty<T>(this IDictionary value)
/// </summary>
/// <param name="value">要判断的字典</param>
/// <returns>判断结果,null或空枚举器返回true,否则返回false</returns>
public static bool IsNullOrEmpty(this IEnumerable value)
public static bool IsNullOrEmpty(this IEnumerable? value)
{
return value == null
|| !value.GetEnumerator().MoveNext();
Expand Down Expand Up @@ -217,5 +220,16 @@ public static bool IsNotNullOrEmpty(this IEnumerable value)
}

#endregion IsNotNull and IsNotNullOrEmpty

/// <summary>
/// 转换为<see cref="Task"/>类型
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static Task<TResult> ToTask<TResult>(this TResult obj)
{
return Task.FromResult(obj);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public static TValue GetOrDefault<TKey, TValue>(
TKey key,
TValue defaultValue = default)
{
key.CheckNullWithException(nameof(key));
if (valuePairs.IsNullOrEmpty()) { return defaultValue; }

bool isExist = valuePairs.TryGetValue(key, out TValue value);
Expand All @@ -37,8 +36,6 @@ public static bool TryAdd<TKey, TValue>(
TKey key,
TValue value)
{
key.CheckNullWithException(nameof(key));
value.CheckNullWithException(nameof(value));
valuePairs.CheckNullWithException(nameof(valuePairs));

bool isExist = valuePairs.ContainsKey(key);
Expand Down Expand Up @@ -83,7 +80,6 @@ public static TValue GetOrAdd<TKey, TValue>(
Func<TValue> factory)

{
key.CheckNullWithException(nameof(key));
valuePairs.CheckNullWithException(nameof(valuePairs));
factory.CheckNullWithException(nameof(factory));

Expand Down Expand Up @@ -168,7 +164,6 @@ public static TValue AddOrUpdate<TKey, TValue>(
TValue value)
{
valuePairs.CheckNullWithException(nameof(valuePairs));
value.CheckNullWithException(nameof(value));

if (valuePairs.ContainsKey(key))
{
Expand Down
17 changes: 1 addition & 16 deletions CkTools/Src/CkTools.BaseExtensions/BaseType/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static string ToJsonExt<T>(this T obj, Type type)

#endregion Tojson

public static T DeepCopy<T>(this T obj)
public static T? DeepCopy<T>(this T obj)
where T : class
{
string outPut = obj.ToJsonExt();
Expand Down Expand Up @@ -175,21 +175,6 @@ private static IEnumerable<KeyValuePair<string, object>> GetAllPropertyValueIter

#region 基础类型与Object之间的转换

/// <summary>
/// 转换为byte[]
/// </summary>
/// <param name="source"> </param>
/// <param name="encoding">编码格式,默认 <see cref="Encoding.UTF8" /></param>
/// <returns></returns>
public static byte[] ToBytes<T>(this T source, Encoding? encoding = null)
{
source.CheckNullWithException(nameof(source));

string objStr = JsonConvert.SerializeObject(source, JsonSerializerSettingConst.StorageSetting);
encoding = encoding ?? Encoding.UTF8;
return objStr.ToBytes(encoding);
}

public static int ToInt32<T>(this T str)
where T : class
{
Expand Down
10 changes: 5 additions & 5 deletions CkTools/Src/CkTools.BaseExtensions/BaseType/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static T ToObjectExt<T>(
this string jsonStr,
JsonSerializerSettings? jsonSerializerSettings = null)
{
jsonSerializerSettings = jsonSerializerSettings ?? JsonSerializerSettingConst.DefaultSetting;
jsonSerializerSettings ??= JsonSerializerSettingConst.DefaultSetting;

return JsonConvert.DeserializeObject<T>(jsonStr, jsonSerializerSettings);
}
Expand Down Expand Up @@ -95,8 +95,8 @@ public static decimal ToDecimal(this string str)
}

public static decimal ToDecimalOrDefault(
this string str,
decimal defaultValue = 0.00M)
this string str,
decimal defaultValue = 0.00M)
{
return str.BaseConvertOrDefalut(System.Convert.ToDecimal, defaultValue);
}
Expand All @@ -107,8 +107,8 @@ public static double ToDouble(this string str)
}

public static double ToDoubleOrDefault(
this string str,
double defaultValue = 0.00)
this string str,
double defaultValue = 0.00)
{
return str.BaseConvertOrDefalut(System.Convert.ToDouble, defaultValue);
}
Expand Down
159 changes: 159 additions & 0 deletions CkTools/Src/CkTools.BaseExtensions/Helper/ExpressionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace CkTools.Helper
{
/// <summary>
/// 表达式帮助类
/// </summary>
public static class ExpressionHelper
{
/// <summary>
/// 创建一个访问属性的表达式
/// </summary>
/// <param name="expression">表达式</param>
/// <param name="propertyName">属性名称</param>
/// <returns></returns>
public static Expression Property(this Expression expression, string propertyName)
{
return Expression.Property(expression, propertyName);
}

/// <summary>
/// 条件表达式且(第一个true才执行第二个)
/// </summary>
/// <param name="left">左表达式</param>
/// <param name="right">右表达式</param>
/// <returns></returns>
public static Expression AndAlso(this Expression left, Expression right)
{
return Expression.AndAlso(left, right);
}

/// <summary>
/// 创建一个回调带有参数方法的表达式
/// </summary>
/// <param name="instance">表达式</param>
/// <param name="methodName">方法名字</param>
/// <param name="arguments">参数</param>
/// <returns></returns>
public static Expression Call(this Expression instance, string methodName, params Expression[] arguments)
{
return Expression.Call(instance, instance.Type.GetMethod(methodName), arguments);
}

/// <summary>
/// 创建一个比较表达式
/// </summary>
/// <param name="left">左表达式</param>
/// <param name="right">右表达式</param>
/// <returns></returns>
public static Expression GreaterThan(this Expression left, Expression right)
{
return Expression.GreaterThan(left, right);
}

/// <summary>
/// Lambda表达式
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="body">表达式</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public static Expression<T> ToLambda<T>(this Expression body, params ParameterExpression[] parameters)
{
return Expression.Lambda<T>(body, parameters);
}

/// <summary>
/// Lambda(真)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> True<T>() { return param => true; }

/// <summary>
/// Lambda(假)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> False<T>() { return param => false; }

/// <summary>
/// 组合And
/// </summary>
/// <returns></returns>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.AndAlso);
}

/// <summary>
/// 组合Or
/// </summary>
/// <returns></returns>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.OrElse);
}

/// <summary>
/// Combines the first expression with the second using the specified merge function.
/// </summary>
private static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
var map = first.Parameters
.Select((f, i) => new { f, s = second.Parameters[i] })
.ToDictionary(p => p.s, p => p.f);
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}

/// <summary>
/// ParameterRebinder
/// </summary>
private class ParameterRebinder : ExpressionVisitor
{
/// <summary>
/// The ParameterExpression map
/// </summary>
private readonly Dictionary<ParameterExpression, ParameterExpression> map;

/// <summary>
/// Initializes a new instance of the <see cref="ParameterRebinder"/> class.
/// </summary>
/// <param name="map">The map.</param>
private ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
{
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
}

/// <summary>
/// Replaces the parameters.
/// </summary>
/// <param name="map">The map.</param>
/// <param name="exp">The exp.</param>
/// <returns>Expression</returns>
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
{
return new ParameterRebinder(map).Visit(exp);
}

/// <summary>
/// Visits the parameter.
/// </summary>
/// <param name="p">The p.</param>
/// <returns>Expression</returns>
protected override Expression VisitParameter(ParameterExpression p)
{
if (map.TryGetValue(p, out ParameterExpression replacement))
{
p = replacement;
}
return base.VisitParameter(p);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public static class TypeConvertDelegate
.Pipe(longToUtcDateTimeOffsetByMilliseconds)
.Pipe(utcToLocal);

public static Func<string, DateTime> stringToLocalDateTime = stringToLong.Pipe(longToDatetime);

#endregion 组合转换
}
}
4 changes: 3 additions & 1 deletion CkTools/Src/CkTools.BaseExtensions/TypeConvertExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public static TValue BaseConvert<T, TValue>(
this T source,
Func<T, TValue> convert)
{
if (typeof(T).IsClass && source == null)
if (typeof(T).IsClass
&& (source == null || source.GetHashCode() == string.Empty.GetHashCode()))
{
throw new ArgumentException($"The parameter is invalid,value:{source}", nameof(source));
}

return convert(source);
}

Expand Down
Loading

0 comments on commit 5faf373

Please sign in to comment.