mirror of
				https://github.com/nsnail/ns-ext.git
				synced 2025-10-31 23:15:28 +08:00 
			
		
		
		
	init
This commit is contained in:
		
							
								
								
									
										89
									
								
								src/.editorconfig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								src/.editorconfig
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,89 @@ | ||||
| [*.{appxmanifest,asax,ascx,aspx,axaml,axml,build,config,cs,cshtml,csproj,css,dbml,discomap,dtd,htm,html,js,json,jsproj,jsx,lsproj,master,njsproj,nuspec,paml,proj,props,proto,razor,resjson,resw,resx,skin,StyleCop,targets,tasks,ts,tsx,vb,vbproj,xaml,xamlx,xml,xoml,xsd}] | ||||
| indent_style = space | ||||
| indent_size = space | ||||
| tab_width = 4 | ||||
|  | ||||
| [*] | ||||
|  | ||||
| # Microsoft .NET properties | ||||
| csharp_indent_braces = false | ||||
| csharp_new_line_before_members_in_object_initializers = true | ||||
| csharp_new_line_before_open_brace = local_functions, methods, types | ||||
| csharp_preferred_modifier_order = public, private, protected, internal, new, abstract, virtual, sealed, override, static, readonly, extern, unsafe, volatile, async:suggestion | ||||
| csharp_style_var_elsewhere = true:suggestion | ||||
| csharp_style_var_for_built_in_types = true:suggestion | ||||
| csharp_style_var_when_type_is_apparent = true:suggestion | ||||
| dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none | ||||
| dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none | ||||
| dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none | ||||
| dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion | ||||
| dotnet_style_predefined_type_for_member_access = true:suggestion | ||||
| dotnet_style_qualification_for_event = false:suggestion | ||||
| dotnet_style_qualification_for_field = false:suggestion | ||||
| dotnet_style_qualification_for_method = false:suggestion | ||||
| dotnet_style_qualification_for_property = false:suggestion | ||||
| dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion | ||||
|  | ||||
| # ReSharper inspection severities | ||||
| resharper_arrange_redundant_parentheses_highlighting = hint | ||||
| resharper_arrange_this_qualifier_highlighting = hint | ||||
| resharper_arrange_type_member_modifiers_highlighting = hint | ||||
| resharper_arrange_type_modifiers_highlighting = hint | ||||
| resharper_built_in_type_reference_style_for_member_access_highlighting = hint | ||||
| resharper_built_in_type_reference_style_highlighting = hint | ||||
| resharper_redundant_base_qualifier_highlighting = warning | ||||
| resharper_suggest_var_or_type_built_in_types_highlighting = hint | ||||
| resharper_suggest_var_or_type_elsewhere_highlighting = hint | ||||
| resharper_suggest_var_or_type_simple_types_highlighting = hint | ||||
|  | ||||
| # ReSharper properties | ||||
| resharper_align_first_arg_by_paren = true | ||||
| resharper_align_linq_query = true | ||||
| resharper_align_multiline_argument = true | ||||
| resharper_align_multiline_array_and_object_initializer = false | ||||
| resharper_align_multiline_calls_chain = true | ||||
| resharper_align_multiline_expression = true | ||||
| resharper_align_multiline_extends_list = true | ||||
| resharper_align_multiline_for_stmt = true | ||||
| resharper_align_multiline_property_pattern = true | ||||
| resharper_align_multiline_switch_expression = true | ||||
| resharper_align_multline_type_parameter_constrains = true | ||||
| resharper_align_multline_type_parameter_list = true | ||||
| resharper_align_tuple_components = true | ||||
| resharper_csharp_align_multiline_parameter = true | ||||
| resharper_csharp_align_multiple_declaration = true | ||||
| resharper_csharp_empty_block_style = together | ||||
| resharper_csharp_indent_size = 4 | ||||
| resharper_csharp_indent_style = space | ||||
| resharper_csharp_insert_final_newline = true | ||||
| resharper_csharp_max_line_length = 120 | ||||
| resharper_csharp_stick_comment = false | ||||
| resharper_csharp_tab_width = 4 | ||||
| resharper_csharp_wrap_arguments_style = chop_if_long | ||||
| resharper_csharp_wrap_before_comma = false | ||||
| resharper_csharp_wrap_chained_method_calls = chop_if_long | ||||
| resharper_csharp_wrap_extends_list_style = chop_if_long | ||||
| resharper_csharp_wrap_parameters_style = chop_if_long | ||||
| resharper_indent_anonymous_method_block = true | ||||
| resharper_indent_nested_fixed_stmt = true | ||||
| resharper_indent_nested_foreach_stmt = true | ||||
| resharper_indent_nested_for_stmt = true | ||||
| resharper_indent_nested_lock_stmt = true | ||||
| resharper_indent_nested_usings_stmt = true | ||||
| resharper_indent_nested_while_stmt = true | ||||
| resharper_indent_preprocessor_if = usual_indent | ||||
| resharper_indent_preprocessor_other = usual_indent | ||||
| resharper_int_align = true | ||||
| resharper_keep_existing_arrangement = false | ||||
| resharper_keep_existing_linebreaks = false | ||||
| resharper_max_initializer_elements_on_line = 1 | ||||
| resharper_outdent_commas = true | ||||
| resharper_outdent_dots = true | ||||
| resharper_wrap_array_initializer_style = chop_always | ||||
| resharper_wrap_before_comma = false | ||||
| resharper_wrap_object_and_collection_initializer_style = chop_always | ||||
|  | ||||
| [*.cs] | ||||
| indent_style = space | ||||
| indent_size = 4 | ||||
| tab_width = 4 | ||||
							
								
								
									
										16
									
								
								src/NSExt.sln
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/NSExt.sln
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
|  | ||||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NSExt", "NSExt\NSExt.csproj", "{54814D1B-C587-4D90-A7BE-4BE1E170D5FC}" | ||||
| EndProject | ||||
| Global | ||||
| 	GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||
| 		Debug|Any CPU = Debug|Any CPU | ||||
| 		Release|Any CPU = Release|Any CPU | ||||
| 	EndGlobalSection | ||||
| 	GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||||
| 		{54814D1B-C587-4D90-A7BE-4BE1E170D5FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||||
| 		{54814D1B-C587-4D90-A7BE-4BE1E170D5FC}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||||
| 		{54814D1B-C587-4D90-A7BE-4BE1E170D5FC}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||||
| 		{54814D1B-C587-4D90-A7BE-4BE1E170D5FC}.Release|Any CPU.Build.0 = Release|Any CPU | ||||
| 	EndGlobalSection | ||||
| EndGlobal | ||||
							
								
								
									
										95
									
								
								src/NSExt.sln.DotSettings
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								src/NSExt.sln.DotSettings
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										34
									
								
								src/NSExt/ByteExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/NSExt/ByteExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| // @program: NSExt | ||||
| // @file: ByteExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| using System.Text; | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class ByteExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     base64编码 | ||||
|     /// </summary> | ||||
|     /// <param name="me">待编码的字节数组</param> | ||||
|     /// <returns>编码后的base64字符串</returns> | ||||
|     public static string Base64(this byte[] me) | ||||
|     { | ||||
|         return Convert.ToBase64String(me); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将字节数组解码成字符串 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字节数组</param> | ||||
|     /// <param name="e">字符串使用的编码方式</param> | ||||
|     /// <returns>解码后的原始字符串</returns> | ||||
|     public static string HexDe(this byte[] me, Encoding e) | ||||
|     { | ||||
|         return e.GetString(me); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										81
									
								
								src/NSExt/DateTimeExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								src/NSExt/DateTimeExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,81 @@ | ||||
| // @program: NSExt | ||||
| // @file: DateTimeExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| // ReSharper disable UnusedMember.Global | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class DateTimeExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     将一个过去时间对象与当前时间相减转换成“xx以前”的字符串,如2秒以前,3天以前 | ||||
|     /// </summary> | ||||
|     /// <param name="me">时间对象</param> | ||||
|     /// <returns>字符串</returns> | ||||
|     public static string TimeAgo(this DateTime me) | ||||
|     { | ||||
|         var ts = DateTime.Now - me; | ||||
|         if (ts.Days > 0) return ts.Days + "天前"; | ||||
|  | ||||
|         if (ts.Hours > 0) return ts.Hours + "小时前"; | ||||
|  | ||||
|         if (ts.Minutes > 0) return ts.Minutes + "分钟前"; | ||||
|  | ||||
|         return ts.Seconds + "秒前"; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     指定时间的世界协调时的unix时间戳形式 | ||||
|     /// </summary> | ||||
|     /// <param name="me">指定时间</param> | ||||
|     /// <returns>unix时间戳</returns> | ||||
|     public static long TimeUnixUtc(this DateTime me) | ||||
|     { | ||||
|         return (me.ToUniversalTime().Ticks - 621355968000000000) / 10000000; | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     指定时间的世界协调时的unix时间戳形式(毫秒) | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <returns></returns> | ||||
|     public static long TimeUnixUtcMs(this DateTime me) | ||||
|     { | ||||
|         return (me.ToUniversalTime().Ticks - 621355968000000000) / 10000; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     // ReSharper disable once InconsistentNaming | ||||
|     public static string yyyyMM(this DateTime me) | ||||
|     { | ||||
|         return me.ToString("yyyy-MM"); | ||||
|     } | ||||
|  | ||||
|     // ReSharper disable once InconsistentNaming | ||||
|     public static string yyyyMMdd(this DateTime me) | ||||
|     { | ||||
|         return me.ToString("yyyy-MM-dd"); | ||||
|     } | ||||
|  | ||||
|     // ReSharper disable once InconsistentNaming | ||||
|     public static string yyyyMMddHHmm(this DateTime me) | ||||
|     { | ||||
|         return me.ToString("yyyy-MM-dd HH:mm"); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     // ReSharper disable once InconsistentNaming | ||||
|     public static string yyyyMMddHHmmss(this DateTime me) | ||||
|     { | ||||
|         return me.ToString("yyyy-MM-dd HH:mm:ss"); | ||||
|     } | ||||
|  | ||||
|     public static string yyyyMMddHHmmssfff(this DateTime me) | ||||
|     { | ||||
|         return me.ToString("yyyy-MM-dd HH:mm:ss.fff"); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										22
									
								
								src/NSExt/DecimalExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/NSExt/DecimalExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| // @program: NSExt | ||||
| // @file: DecimalExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class DecimalExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     四舍五入后的近似值 | ||||
|     /// </summary> | ||||
|     /// <param name="me">数字</param> | ||||
|     /// <param name="place">小数点位数</param> | ||||
|     /// <returns>处理后的值</returns> | ||||
|     public static decimal Round(this decimal me, int place) | ||||
|     { | ||||
|         var dec = Math.Round(me, place); | ||||
|         return dec; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										25
									
								
								src/NSExt/EnumExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/NSExt/EnumExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| // @program: NSExt | ||||
| // @file: EnumExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| using System.ComponentModel; | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class EnumExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     获取枚举的description属性 | ||||
|     /// </summary> | ||||
|     /// <param name="e">枚举对象</param> | ||||
|     /// <returns>description属性</returns> | ||||
|     public static string Desc(this Enum e) | ||||
|     { | ||||
|         var t     = e.GetType(); | ||||
|         var fi    = t.GetField(Enum.GetName(t, e)!); | ||||
|         var attrs = (DescriptionAttribute[])fi!.GetCustomAttributes(typeof(DescriptionAttribute), false); | ||||
|         return (attrs.Length != 0 ? attrs[0].Description : Enum.GetName(t, e)) ?? ""; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										32
									
								
								src/NSExt/EnumerableExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/NSExt/EnumerableExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| // @program: NSExt | ||||
| // @file: EnumerableExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class EnumerableExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     将列表转成分隔符分隔的字符串 | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <param name="separator"></param> | ||||
|     /// <returns></returns> | ||||
|     public static string Join(this IEnumerable<object> me, string separator) | ||||
|     { | ||||
|         return string.Join(separator, me); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     判断对象是否为null或不存在子元素(如果为集合对象) | ||||
|     /// </summary> | ||||
|     /// <typeparam name="T">对象类型</typeparam> | ||||
|     /// <param name="me">指定对象</param> | ||||
|     /// <returns>空则返回true</returns> | ||||
|     public static bool NullOrEmpty<T>(this IEnumerable<T> me) | ||||
|     { | ||||
|         return me is null || !me.Any(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										49
									
								
								src/NSExt/GenericExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/NSExt/GenericExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,49 @@ | ||||
| // @program: NSExt | ||||
| // @file: GenericExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class GenericExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     从指定的对象拷贝属性 | ||||
|     /// </summary> | ||||
|     /// <typeparam name="T">对象类型</typeparam> | ||||
|     /// <param name="me">拷贝目标</param> | ||||
|     /// <param name="copyObj">拷贝来源</param> | ||||
|     /// <param name="propNameList">需要处理的属性名</param> | ||||
|     /// <param name="isIncludeOrExclude">True包含,false排除</param> | ||||
|     public static void CopyFrom<T>(this T        me, | ||||
|                                    T             copyObj, | ||||
|                                    IList<string> propNameList       = null, | ||||
|                                    bool          isIncludeOrExclude = false) | ||||
|     { | ||||
|         foreach (var p in me.GetType().GetProperties()) { | ||||
|             if (!p.CanWrite) continue; | ||||
|             bool isSet; | ||||
|             if (isIncludeOrExclude) | ||||
|                 isSet = propNameList?.Contains(p.Name) ?? false; | ||||
|             else | ||||
|                 isSet = !propNameList?.Contains(p.Name) ?? true; | ||||
|  | ||||
|             if (isSet) p.SetValue(me, copyObj.GetType().GetProperty(p.Name)?.GetValue(copyObj, null), null); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     判断是否与某对象相等 | ||||
|     /// </summary> | ||||
|     /// <typeparam name="T"></typeparam> | ||||
|     /// <param name="me"></param> | ||||
|     /// <param name="compare"></param> | ||||
|     /// <param name="ret"></param> | ||||
|     /// <returns></returns> | ||||
|     public static T Is<T>(this T me, T compare, T ret) where T : struct | ||||
|     { | ||||
|         return me.Equals(compare) ? ret : me; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										37
									
								
								src/NSExt/IntExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/NSExt/IntExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| // @program: NSExt | ||||
| // @file: IntExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class IntExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     判断枚举是否包含某个位 | ||||
|     /// </summary> | ||||
|     /// <typeparam name="T"></typeparam> | ||||
|     /// <param name="me"></param> | ||||
|     /// <param name="flag"></param> | ||||
|     /// <returns></returns> | ||||
|     public static bool HasFlag<T>(this int me, T flag) where T : Enum | ||||
|     { | ||||
|         return ((long)me).HasFlag(flag); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     生成随机数 | ||||
|     /// </summary> | ||||
|     /// <param name="me">大于等于[0],小于[1]</param> | ||||
|     /// <returns></returns> | ||||
|     public static int Rand(this int[] me) | ||||
|     { | ||||
|         return new Random(Guid.NewGuid().GetHashCode()).Next(me[0], me[1]); | ||||
|     } | ||||
|  | ||||
|     public static string ToIpV4(this int me) | ||||
|     { | ||||
|         return string.Join(".", BitConverter.GetBytes(me).Reverse()); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										76
									
								
								src/NSExt/LoggerExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								src/NSExt/LoggerExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,76 @@ | ||||
| // @program: NSExt | ||||
| // @file: LoggerExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| using System.Runtime.CompilerServices; | ||||
| using Microsoft.Extensions.Logging; | ||||
|  | ||||
| // ReSharper disable UnusedMember.Global | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class LoggerExtensions | ||||
| { | ||||
|     private static string CallerInfoMessage(object message, | ||||
|                                             string callerName, | ||||
|                                             string callerFilePath, | ||||
|                                             int    callerLineNumber) | ||||
|     { | ||||
|         return | ||||
|             $"[{Thread.CurrentThread.ManagedThreadId}#{callerName}@{Path.GetFileName(callerFilePath)}:{callerLineNumber}] {message}"; | ||||
|     } | ||||
|  | ||||
|     public static void Debug(this ILogger              me, | ||||
|                              object                    message, | ||||
|                              [CallerMemberName] string callerName       = null, | ||||
|                              [CallerFilePath]   string callerFilePath   = null, | ||||
|                              [CallerLineNumber] int    callerLineNumber = 0) | ||||
|     { | ||||
|         // ReSharper disable once TemplateIsNotCompileTimeConstantProblem | ||||
|         me.LogDebug(CallerInfoMessage(message, callerName, callerFilePath, callerLineNumber)); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public static void Error(this ILogger              me, | ||||
|                              object                    message, | ||||
|                              [CallerMemberName] string callerName       = null, | ||||
|                              [CallerFilePath]   string callerFilePath   = null, | ||||
|                              [CallerLineNumber] int    callerLineNumber = 0) | ||||
|     { | ||||
|         // ReSharper disable once TemplateIsNotCompileTimeConstantProblem | ||||
|         me.LogError(CallerInfoMessage(message, callerName, callerFilePath, callerLineNumber)); | ||||
|     } | ||||
|  | ||||
|     public static void Fatal(this ILogger              me, | ||||
|                              object                    message, | ||||
|                              [CallerMemberName] string callerName       = null, | ||||
|                              [CallerFilePath]   string callerFilePath   = null, | ||||
|                              [CallerLineNumber] int    callerLineNumber = 0) | ||||
|     { | ||||
|         // ReSharper disable once TemplateIsNotCompileTimeConstantProblem | ||||
|         me.LogCritical(CallerInfoMessage(message, callerName, callerFilePath, callerLineNumber)); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public static void Info(this ILogger              me, | ||||
|                             object                    message, | ||||
|                             [CallerMemberName] string callerName       = null, | ||||
|                             [CallerFilePath]   string callerFilePath   = null, | ||||
|                             [CallerLineNumber] int    callerLineNumber = 0) | ||||
|     { | ||||
|         // ReSharper disable once TemplateIsNotCompileTimeConstantProblem | ||||
|         me.LogInformation(CallerInfoMessage(message, callerName, callerFilePath, callerLineNumber)); | ||||
|     } | ||||
|  | ||||
|     public static void Warn(this ILogger              me, | ||||
|                             object                    message, | ||||
|                             [CallerMemberName] string callerName       = null, | ||||
|                             [CallerFilePath]   string callerFilePath   = null, | ||||
|                             [CallerLineNumber] int    callerLineNumber = 0) | ||||
|     { | ||||
|         // ReSharper disable once TemplateIsNotCompileTimeConstantProblem | ||||
|         me.LogWarning(CallerInfoMessage(message, callerName, callerFilePath, callerLineNumber)); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										33
									
								
								src/NSExt/LongExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/NSExt/LongExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | ||||
| // @program: NSExt | ||||
| // @file: LongExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class LongExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     判断枚举是否包含某个位 | ||||
|     /// </summary> | ||||
|     /// <typeparam name="T"></typeparam> | ||||
|     /// <param name="me"></param> | ||||
|     /// <param name="flag"></param> | ||||
|     /// <returns></returns> | ||||
|     public static bool HasFlag<T>(this long me, T flag) where T : Enum | ||||
|     { | ||||
|         var val = (long)(object)flag; | ||||
|         return (me & val) == val; | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     1970毫秒数转换成日期对象 | ||||
|     /// </summary> | ||||
|     /// <param name="msFrom1970"></param> | ||||
|     /// <returns></returns> | ||||
|     public static DateTime Time(this long msFrom1970) | ||||
|     { | ||||
|         return new DateTime(1970, 1, 1).AddMilliseconds(msFrom1970).ToLocalTime(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										15
									
								
								src/NSExt/NSExt.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/NSExt/NSExt.csproj
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
|  | ||||
|     <PropertyGroup> | ||||
|         <TargetFramework>net6.0</TargetFramework> | ||||
|         <ImplicitUsings>enable</ImplicitUsings> | ||||
|     </PropertyGroup> | ||||
|  | ||||
|     <ItemGroup> | ||||
|         <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0"/> | ||||
|         <PackageReference Include="Newtonsoft.Json" Version="13.0.1"/> | ||||
|         <PackageReference Include="SshNet.Security.Cryptography" Version="1.3.0"/> | ||||
|     </ItemGroup> | ||||
|  | ||||
|  | ||||
| </Project> | ||||
							
								
								
									
										23
									
								
								src/NSExt/ObjectExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/NSExt/ObjectExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| // @program: NSExt | ||||
| // @file: ObjectExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class ObjectExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     将一个对象序列化成json文本 | ||||
|     /// </summary> | ||||
|     /// <param name="me">指定对象</param> | ||||
|     /// <param name="format">是否格式化</param> | ||||
|     /// <returns>json文本</returns> | ||||
|     public static string Json(this object me, bool format = false) | ||||
|     { | ||||
|         return JsonConvert.SerializeObject(me, format ? Formatting.Indented : Formatting.None); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										390
									
								
								src/NSExt/StringExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										390
									
								
								src/NSExt/StringExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,390 @@ | ||||
| // @program: NSExt | ||||
| // @file: StringExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| using System.Globalization; | ||||
| using System.Text; | ||||
| using System.Text.RegularExpressions; | ||||
| using System.Web; | ||||
| using Newtonsoft.Json; | ||||
| using Newtonsoft.Json.Linq; | ||||
| using SshNet.Security.Cryptography; | ||||
| using HMACMD5 = System.Security.Cryptography.HMACMD5; | ||||
|  | ||||
| // ReSharper disable UnusedMember.Global | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class StringExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     base64编码 | ||||
|     /// </summary> | ||||
|     /// <param name="me">待base64编码的字符串</param> | ||||
|     /// <param name="e">字符串的编码方式</param> | ||||
|     /// <returns>编码后的base64字符串</returns> | ||||
|     public static string Base64(this string me, Encoding e) | ||||
|     { | ||||
|         return e.GetBytes(me).Base64(); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     base64解码 | ||||
|     /// </summary> | ||||
|     /// <param name="me">待解码的字符串</param> | ||||
|     /// <returns>解码后的原始字节数组</returns> | ||||
|     public static byte[] Base64De(this string me) | ||||
|     { | ||||
|         return Convert.FromBase64String(me); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     base64解码 | ||||
|     /// </summary> | ||||
|     /// <param name="me">待解码的字符串</param> | ||||
|     /// <param name="e">字符串的编码方式</param> | ||||
|     /// <returns>解码后的原始字符串</returns> | ||||
|     public static string Base64De(this string me, Encoding e) | ||||
|     { | ||||
|         return e.GetString(Base64De(me)); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将易于web传输的base64web字符串转换为原生base64 | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <returns>原生base64</returns> | ||||
|     public static string Base64Sys(this string me) | ||||
|     { | ||||
|         return me.Replace("-", "+").Replace("_", "/").Replace(".", "="); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将原生base64字符串转换成易于web传输的字符串 | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <returns>易于web传输的字符串</returns> | ||||
|     public static string Base64Web(this string me) | ||||
|     { | ||||
|         return me.Replace("+", "-").Replace("/", "_").Replace("=", "."); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将字符串转换成日期对象 | ||||
|     /// </summary> | ||||
|     /// <param name="me">待转换字符串</param> | ||||
|     /// <returns>转换后的日期对象</returns> | ||||
|     public static DateTime DateTime(this string me) | ||||
|     { | ||||
|         return System.DateTime.Parse(me, CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将字符串转换成日期对象 | ||||
|     /// </summary> | ||||
|     /// <param name="me">待转换字符串</param> | ||||
|     /// <param name="def">转换失败时返回的日期对象</param> | ||||
|     /// <returns>转换后的日期对象</returns> | ||||
|     public static DateTime DateTimeTry(this string me, DateTime def) | ||||
|     { | ||||
|         return !System.DateTime.TryParse(me, out var ret) ? def : ret; | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     string to decimal | ||||
|     /// </summary> | ||||
|     /// <param name="me">string</param> | ||||
|     /// <returns>decimal</returns> | ||||
|     public static decimal Dec(this string me) | ||||
|     { | ||||
|         return decimal.Parse(me, CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     尝试将字符串转为decimal | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <param name="def">转换失败后返回的默认值</param> | ||||
|     /// <returns>转换后的decimal</returns> | ||||
|     public static decimal DecTry(this string me, decimal def) | ||||
|     { | ||||
|         return !decimal.TryParse(me, out var ret) ? def : ret; | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将字符串转换成枚举对象 | ||||
|     /// </summary> | ||||
|     /// <typeparam name="T"></typeparam> | ||||
|     /// <param name="name"></param> | ||||
|     /// <returns></returns> | ||||
|     public static T Enum<T>(this string name) where T : Enum | ||||
|     { | ||||
|         return (T)System.Enum.Parse(typeof(T), name); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将字符串转换成枚举对象 | ||||
|     /// </summary> | ||||
|     /// <typeparam name="T"></typeparam> | ||||
|     /// <param name="name"></param> | ||||
|     /// <param name="def"></param> | ||||
|     /// <returns></returns> | ||||
|     public static T EnumTry<T>(this string name, T def) where T : Enum | ||||
|     { | ||||
|         return !System.Enum.TryParse(typeof(T), name, out var ret) ? def : (T)ret; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将字符串转为guid | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <returns></returns> | ||||
|     public static Guid Guid(this string me) | ||||
|     { | ||||
|         return System.Guid.Parse(me); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将字符串转换成guid | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <param name="def">转换失败的返回值</param> | ||||
|     /// <returns></returns> | ||||
|     public static Guid Guid(this string me, Guid def) | ||||
|     { | ||||
|         return System.Guid.TryParse(me, out var ret) ? ret : def; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将字符串转换成字节数组形式 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <param name="e">字符串使用的编码</param> | ||||
|     /// <returns>字节数组</returns> | ||||
|     public static byte[] Hex(this string me, Encoding e) | ||||
|     { | ||||
|         return e.GetBytes(me); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     解码html编码 | ||||
|     /// </summary> | ||||
|     /// <param name="me">html编码后的字符串</param> | ||||
|     /// <returns>解码后的原始字符串</returns> | ||||
|     public static string HtmlDe(this string me) | ||||
|     { | ||||
|         return HttpUtility.HtmlDecode(me); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     string to Int32 | ||||
|     /// </summary> | ||||
|     /// <param name="me">string</param> | ||||
|     /// <returns>Int32</returns> | ||||
|     public static int Int32(this string me) | ||||
|     { | ||||
|         return int.Parse(me, CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     尝试将字符串转为int32 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <param name="def">转换失败后返回的默认值</param> | ||||
|     /// <returns>转换后的int32</returns> | ||||
|     public static int Int32Try(this string me, int def) | ||||
|     { | ||||
|         return !int.TryParse(me, out var ret) ? def : ret; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     string to Int64 | ||||
|     /// </summary> | ||||
|     /// <param name="me">string</param> | ||||
|     /// <returns>Int64</returns> | ||||
|     public static long Int64(this string me) | ||||
|     { | ||||
|         return long.Parse(me, CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     尝试将字符串转为int64 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <param name="def">转换失败后返回的默认值</param> | ||||
|     /// <returns>转换后的int64</returns> | ||||
|     public static long Int64Try(this string me, long def) | ||||
|     { | ||||
|         return !long.TryParse(me, out var ret) ? def : ret; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将一个json字符串反序列化成为jObject对象 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <returns></returns> | ||||
|     public static JObject JObject(this string me) | ||||
|     { | ||||
|         return Newtonsoft.Json.Linq.JObject.Parse(me); | ||||
|     } | ||||
|  | ||||
|     public static string MaskChineseName(this string me) | ||||
|     { | ||||
|         if (me.Length == 2) return "*" + me[1..]; | ||||
|  | ||||
|         return me[..1] + "*" + me[^1..]; | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     对一个手机号进行掩码处理 | ||||
|     /// </summary> | ||||
|     /// <param name="me">手机号</param> | ||||
|     /// <returns>掩码后的手机号</returns> | ||||
|     public static string MaskMobile(this string me) | ||||
|     { | ||||
|         return new Regex(@"^(\d{3})\d{4}(\d{4})$").Replace(me, "$1****$2"); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     对一个字符串进行md5hash运算 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <param name="e">字符串使用的编码</param> | ||||
|     /// <returns>hash摘要的16进制文本形式(无连字符小写)</returns> | ||||
|     public static string Md5(this string me, Encoding e) | ||||
|     { | ||||
|         using var md5 = new MD5(); | ||||
|         return BitConverter.ToString(md5.ComputeHash(e.GetBytes(me))) | ||||
|                            .Replace("-", string.Empty) | ||||
|                            .ToLower(CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     MD5 hmac编码 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <param name="key">密钥</param> | ||||
|     /// <param name="e">字符串使用的编码</param> | ||||
|     /// <returns>hash摘要的16进制文本形式(无连字符小写)</returns> | ||||
|     private static string Md5Hmac(this string me, string key, Encoding e) | ||||
|     { | ||||
|         using var md5Hmac = new HMACMD5(e.GetBytes(key)); | ||||
|         return BitConverter.ToString(md5Hmac.ComputeHash(e.GetBytes(me))) | ||||
|                            .Replace("-", string.Empty) | ||||
|                            .ToLower(CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     判断字符串是否为null或不存在子元素(如果为集合对象);如果为空,返回指定的默认值,否则返回字符串本身 | ||||
|     /// </summary> | ||||
|     /// <param name="me">指定字符串</param> | ||||
|     /// <param name="defVal">指定的默认值</param> | ||||
|     /// <returns>如果为空,返回指定的默认值,否则返回字符串本身</returns> | ||||
|     public static string NullOrEmpty(this string me, string defVal) | ||||
|     { | ||||
|         return me.AsEnumerable().NullOrEmpty() ? defVal : me; | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     反序列化一个文件获得指定类型的数据对象 | ||||
|     /// </summary> | ||||
|     /// <param name="me">等待反序列化的json文本</param> | ||||
|     /// <returns>反序列化后生成的对象</returns> | ||||
|     public static T Object<T>(this string me) | ||||
|     { | ||||
|         return JsonConvert.DeserializeObject<T>(me); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     生成密码 | ||||
|     /// </summary> | ||||
|     /// <param name="me">密码原文</param> | ||||
|     /// <returns>密文</returns> | ||||
|     public static string Pwd(this string me) | ||||
|     { | ||||
|         return me.Md5Hmac(me.Md5(Encoding.UTF8), Encoding.UTF8); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     移除字符串中的html标签 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <returns>处理之后的字符串</returns> | ||||
|     public static string RemoveHtmlTag(this string me) | ||||
|     { | ||||
|         return new Regex(@"<[^>]*>").Replace(me, ""); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     删除换行符 | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <returns></returns> | ||||
|     public static string RemoveWrapped(this string me) | ||||
|     { | ||||
|         return me.Replace("\r", "").Replace("\n", ""); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     截取指定长度的字符串,代替substring | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <param name="startIndex"></param> | ||||
|     /// <param name="length"></param> | ||||
|     /// <returns></returns> | ||||
|     public static string Sub(this string me, int startIndex, int length) | ||||
|     { | ||||
|         if (startIndex + length > me.Length) length = me.Length - startIndex; | ||||
|         return me.Substring(startIndex, length); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将连续多个空格替换成一个空格 | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <returns></returns> | ||||
|     public static string TrimSpaces(this string me) | ||||
|     { | ||||
|         var ret = me.Replace("  ", " "); | ||||
|         // ReSharper disable once TailRecursiveCall | ||||
|         return ret == me ? ret : TrimSpaces(ret); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     url编码 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <returns>url编码后的字符串</returns> | ||||
|     public static string Url(this string me) | ||||
|     { | ||||
|         return Uri.EscapeDataString(me); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     解码url编码 | ||||
|     /// </summary> | ||||
|     /// <param name="me">url编码后的字符串</param> | ||||
|     /// <returns>解码后的原始字符串</returns> | ||||
|     public static string UrlDe(this string me) | ||||
|     { | ||||
|         return Uri.UnescapeDataString(me); | ||||
|     } | ||||
|  | ||||
|     public static int IpV4ToInt32(this string me) | ||||
|     { | ||||
|         return BitConverter.ToInt32(me.Split('.').Select(byte.Parse).Reverse().ToArray(), 0); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										20
									
								
								src/NSExt/UriExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/NSExt/UriExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| // @program: NSExt | ||||
| // @file: UriExtensions.cs | ||||
| // @author: tao ke | ||||
| // @mailto: taokeu@gmail.com | ||||
| // @created: 07/15/2022 16:49 | ||||
|  | ||||
| namespace NSExt; | ||||
|  | ||||
| public static class UriExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     移除url的Scheme | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <returns></returns> | ||||
|     public static string RemoveScheme(this Uri me) | ||||
|     { | ||||
|         return "//" + me.Authority + me.PathAndQuery; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										1
									
								
								src/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
|  | ||||
		Reference in New Issue
	
	Block a user