Koubot.Tool 2.0.1-alpha

This is a prerelease version of Koubot.Tool.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Koubot.Tool --version 2.0.1-alpha
                    
NuGet\Install-Package Koubot.Tool -Version 2.0.1-alpha
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Koubot.Tool" Version="2.0.1-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Koubot.Tool" Version="2.0.1-alpha" />
                    
Directory.Packages.props
<PackageReference Include="Koubot.Tool" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Koubot.Tool --version 2.0.1-alpha
                    
#r "nuget: Koubot.Tool, 2.0.1-alpha"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Koubot.Tool@2.0.1-alpha
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Koubot.Tool&version=2.0.1-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Koubot.Tool&version=2.0.1-alpha&prerelease
                    
Install as a Cake Tool

Language

English | 简体中文

Koubot.Tool

A toolkit to assist in the development of Koubot related projects, providing a large number of extension methods, as well as some tool classes, can also be used to develop other projects (for lazy people lol), has also been uploaded to Nuget, you can use Nuget search Koubot to reference. The toolkit project does not have and will not have other project dependencies.

bases on .NET Standard 2.0

Some of the methods inside use features of VS plug-in ReSharper, and good hints and code checking are available when using ReSharper

  	//Ability to teach ReSharper null judgment (passed in null, returns true)		
  	[ContractAnnotation("null => true")] 
        public static bool IsNullOrEmpty([CanBeNull] this string s)
            => string.IsNullOrEmpty(s);
//https://www.jetbrains.com/help/resharper/Contract_Annotations.html#syntax

General

KouTaskDelayer

Timer that executes a task at particular time. if used it, it will takes a long-running thread to determine whether there is a task in the task list that needs to be executed. Supports same execution times.

KouTaskDelayer.AddTask(executeDate, new Task(() => { ... }));

SortTool

Extension methods in it can help to quickly complete the implementation of Comparison, Compare and other related methods, and it support chaining (to achieve weight sorting) and compare with null.

		public int CompareTo(SystemAliasList? other)
        {
            return this.CompareToObjAsc(IsGlobalAlias, other?.IsGlobalAlias, out int result)
                ?.CompareToObjAsc(IsGroupAlias, other?.IsGroupAlias, out result)
                ?.CompareToObjAsc(Advanced, other?.Advanced, out result)
                ?.CompareToObjAsc(AliasId, other?.AliasId, out result) == null ? result : 0;
        }

For example, to implement a CompareTo method for alias list, the effect is that the list will first sorted by the IsGlobalAlias field in ascending order, then by the IsGroupAlias field ascending, then by the Advanced field ascending , and finally by the AliasID field ascending, and the empty ones will be placed at the end (represent the null comparison support)

KouWatch

Unit test timer/efficiency comparator

When you need to time the execution time, you always need to write:

Stopwatch watch = new Stopwatch();
watch.Start();
...
watch.Stop();
Debug.WriteLine(watch.ElapsedMilliseconds);

Obviously very tedious, if you use KouWatch, you can use the following instead, and it will automatically output the time when the execution is finished.

			KouWatch.Start("testName", () =>
            {
                ...
            });

In addition, if you want to compare the execution time, there are methods for comparing, such as testing the efficiency of GetCustomAttributeCached compared to the system method.

			KouWatch.Start("cache", () =>
            {
                userBlacklist.FieldInfo(p => p.Reason);//二次封装GetCustomAttributeCached
            }, "default", () =>
            {
                property.GetCustomAttribute<KouAutoModelField>(); ;
            }, 100000);
//Output results.
“cache”动作执行100000次中...
cache执行时间:253ms
“default”动作执行100000中...
default执行时间:596ms
效率比较:“cache”动作比default快135.573%倍

Web

LeakyBucketRateLimiter

A leaky bucket algorithm flow limiter specifically for client-side API calls. The common implementation of the Leaky Bucket algorithm on the web does not calculate the time it takes for a request to reach the server, causing it easily to exceed the QPS specified by the server. This flow limiter implementation takes the request out of the bucket only after it is complete.

QPS support floating point number, such as 0.5, that is, 2 seconds before a call (in addition, if the QPS is very large, the interval call once will be at most 50ms , so the current maximum effective support for QPS for about 20 (later to optimize)) error in 0.5ms - 3ms or so

In addition, the flow limiter supports multiple APIs to limit the flow simultaneously and does not open new threads for automatic bucket leakage, but uses these threads to automatically control the requests in the bucket (but this also creates the disadvantage of poor support for large QPS)

support setting timeout time

Usage example:

using (var limiter = new LeakyBucketRateLimiter("test", TestQPS, TestLimitedSize))
{
    if (!limiter.CanRequest())
    {
        Console.WriteLine($"Request Failed: {limiter.ErrorMsg}");
        return;
    }
    Console.WriteLine($"An API request is sent");
}

The IDisposable feature is utilized so that when the request is out of the using range, the request is automatically considered finished and the request can be removed from the bucket

Encoder/Decoder

Currently provides base64 encryption and decryption (WebTool.EncodeBase64), and MD5 encryption (WebTool.EncryptStringMD5).

Random

RandomTool

Initialize only once and always use the same random number seed.

Those starting with XXX. are extension methods of the corresponding type

Method Description
IList.RandomGetOne Get a random item from IList and return default(T) if it fails
Array.RandomGetOne Get a random item from the Array and return default(T) if it fails
IList.RandomGetItems Randomly get a specified number of items (will not repeat), return list, failure returns null
IList.RandomList Disrupt the IList order and return it, or return the original list if it fails
IList.EnumRandomGetOne Randomly select one from Enum (need Enum class to be 0-n continuous)
IntervalDoublePair.GenerateRandomDouble Generate random double in an interval range (used with the IntervalDoublePair class in Tool)
GenerateRandomDouble Generate random double in an interval range
T.ProbablyDo There is an x% probability of not returning null, that is, there is an x% probability that it will do it (use ? on the chain truncation, for probabilistic execution)
T.ProbablyBe There is x% chance that the given object will be giving T
double.ProbablyTrue Returns true with x% probability
GetSecurityRandomByte Get a strong random byte array
GetRandomString Generate a random string
... ...

Math

ExpressionCalculator

Expression calculator that can be used to calculate expressions like sin(pi/2)^e-1*floor(3.5).

IntervalDoublePair

Based on the IntervalDouble class, it can convert strings such as (1,3], (-1,) into intervals and determine the inclusion relationship between intervals.

Interfaces

IKouErrorMsg

Some service classes that implement this interface can get the reason for the error returned.

String

KouStringTool

string into the corresponding type , mostly based on the regular implementation . (actually a partial open source implementation of the KouType type system)

Hope to evolve to use NLP (?)

Method Or Class Description
ZhNumber Tools related to the conversion of Chinese numeric strings to Arabic numbers, such as the ability to replace all Chinese numbers in a string with Arabic numbers
KouStringTool.TryToBool Support all kinds of words that express affirmative or negative to bool
KouStringTool.TryToDouble Support Chinese as well as units and even expressions, such as: 壹佰;500;1k、1w;一万;(sin(pi/2)^e*100+14)*1000+五百一十肆, etc.
KouStringTool.TryToInt Similar to Double, but truncates the decimal part
KouStringTool.TryToEnum Converts a string to its corresponding enum type. Use with the KouEnumName feature tag for best results.
KouStringTool.TryGetTimeSpanInterval String to time interval, for example 13:41-18:07:22, you can get TimeSpan of 13:41 and TimeSpan of 18:07:22
KouStringTool.TryToTimeSpan 一炷香(30 minutes),一小时,1h5m(one hour and five minutes),1:00(one minute),50(50 seconds),后天早上八点(automatically calculates the current distance to 8:00 a.m. the day after), DateTime format, etc.

Format

Use

var result = $"{Reason?.Be($"\nReason:{Reason}")}";

instead of

var result = $"{(Reason == null ? null : $"\nReason:{Reason}" )}";

There are also obj.BeIfNotEmptyobj.BeIfNotWhiteSpaceobj.BeIfTrueobj.BeIfNotDefault

SystemExpand

GenericExpand

Method Help
T.EqualsAny Determines if there exists an element given as equal to it.
T.EqualsAll Those two work well when comparing more than one. <br>for example, Blog.Post.Name == "a" || Blog.Post.Name == "b" || Blog.Post.Name == "c" can be replaced by Blog.Post.Name.EqualsAny("a","b","c")
T.SatisfyAny delegate version
... ...

IDictionary Expand

ContainsAny、ContainsAll、GetValueOrCustom... (The official GetValueOrDefault isn't convenient, because it cannot pass null)...

Reflection

T.CloneParameters(T copyObj):Clone all property values in an object to the object (EFCore will track the changes because the Action operation is done) (can be set to ignore the cloned property names)

Attribute

CustomAttributeExtensions encapsulates some extension methods about CustomAttribute, using Dictionary as cache, to efficiently get the value of the custom tag (Attribute) used by the user on the class.

The GetCustomAttributeCached method can quickly get the custom attribute on the corresponding class or the specified attribute or method, compared with the self-contained GetCustomAttribute, no need to use reflection to get the Type of the specified attribute or method in the class to get the custom attribute, and about two times more efficient.

Enum

GetDescription

GetFlagsDescription

Remove

Add

HasAnyFlag

HasTheFlag (make good use of IDE hint)

HasAllFlag

Int/Double

LimitInRange

Type

Type.IsNullableValueType...

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Koubot.Tool:

Package Downloads
Koubot.SDK

SDK for building Koubot plugins or adapters.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.5-alpha 136 4/1/2024
2.0.3-alpha 293 6/20/2023
2.0.2-alpha 237 12/8/2022
2.0.1-alpha 281 6/29/2022
2.0.0-alpha 253 6/6/2022
1.0.5 583 3/28/2021
1.0.4 472 3/15/2021
1.0.3 459 2/9/2021
1.0.2 443 1/27/2021
1.0.1 596 8/14/2020
1.0.0 620 8/14/2020

v2.0.1-alpha update
[Json] Add some extensions for System.Text.Json.
[KouHttp] Support binary file body post.
[General] Imporve some methods.
[Hint] Hint language continuing migrating to English.