Rystem 6.0.4

dotnet add package Rystem --version 6.0.4
NuGet\Install-Package Rystem -Version 6.0.4
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="Rystem" Version="6.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rystem --version 6.0.4
#r "nuget: Rystem, 6.0.4"
#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.
// Install Rystem as a Cake Addin
#addin nuget:?package=Rystem&version=6.0.4

// Install Rystem as a Cake Tool
#tool nuget:?package=Rystem&version=6.0.4

What is Rystem?

Get Started

Extension methods

Stopwatch

You can monitor the time spent on an action, task or in a method. Some examples from Unit test.

var started = Stopwatch.Start();
//do something
await Task.Delay(2000);
var result = started.Stop();

or

var result = await Stopwatch.MonitorAsync(async () =>
{
    await Task.Delay(2000);
});

or with a return value

 var result = await Stopwatch.MonitorAsync(async () =>
{
    await Task.Delay(2000);
    return 3;
});

Linq expression serializer

Usually a linq expression is not serializable as string. With this method you can serialize your expression with some limits. Only primitives are allowed in the expression body. An example from Unit test.

Expression<Func<MakeIt, bool>> expression = ƒ => ƒ.X == q && ƒ.Samules.Any(x => x == k) && ƒ.Sol && (ƒ.X.Contains(q) || ƒ.Sol.Equals(IsOk)) && (ƒ.E == id | ƒ.Id == V) && (ƒ.Type == MakeType.Yes || ƒ.Type == qq);
var serialized = expression.Serialize();

with result

"ƒ => ((((((ƒ.X == \"dasda\") AndAlso ƒ.Samules.Any(x => (x == \"ccccde\"))) AndAlso ƒ.Sol) AndAlso (ƒ.X.Contains(\"dasda\") OrElse ƒ.Sol.Equals(True))) AndAlso ((ƒ.E == Guid.Parse(\"bf46510b-b7e6-4ba2-88da-cef208aa81f2\")) Or (ƒ.Id == 32))) AndAlso ((ƒ.Type == 1) OrElse (ƒ.Type == 2)))"

with deserialization

var newExpression = expressionAsString.Deserialize<MakeIt, bool>();

and usage, for instance, with Linq

var result = makes.Where(newExpression.Compile()).ToList();

you can deserialize and compile at the same time with

var newExpression = expressionAsString.DeserializeAndCompile<MakeIt, bool>();

you can deserialize as dynamic and use the linq dynamic methods

Expression<Func<MakeIt, int>> expression = x => x.Id;
string value = expression.Serialize();
LambdaExpression newLambda = value.DeserializeAsDynamic<MakeIt>();
var got = makes.AsQueryable();
var cut = got.OrderByDescending(newLambda).ThenByDescending(newLambda).ToList();

please see the unit test here to understand better how it works You may deal with return type of your lambda expression:

 LambdaExpression newLambda = value.DeserializeAsDynamic<MakeIt>();
 newLambda =  newLambda.ChangeReturnType<bool>();

or

 newLambda =  newLambda.ChangeReturnType(typeof(bool));

Reflection helper

Name of calling class

You can find the name of the calling class from your method, with deep = 1 the calling class of your method, with deep = 2 the calling class that calls the class that calls your method, and so on, with fullName set to true you obtain the complete name of the discovered class.

ReflectionHelper.NameOfCallingClass(deep, fullName);
Extensions for Type class

You can get the properties, fields and constructors for your class (and singleton them to save time during new requests)

Type.FetchProperties();
Type.FecthConstructors();
Type.FetchFields();

You can check if a Type is a son or a father or both of other type (in the example Zalo and Folli are Sulo). You may find more information in unit test here

Zalo zalo = new();
Zalo zalo2 = new();
Folli folli = new();
Sulo sulo = new();
object quo = new();
int x = 2;
decimal y = 3;
Assert.True(zalo.IsTheSameTypeOrASon(sulo));
Assert.True(folli.IsTheSameTypeOrASon(sulo));
Assert.True(zalo.IsTheSameTypeOrASon(zalo2));
Assert.True(zalo.IsTheSameTypeOrASon(quo));
Assert.False(sulo.IsTheSameTypeOrASon(zalo));
Assert.True(sulo.IsTheSameTypeOrAParent(zalo));
Assert.False(y.IsTheSameTypeOrAParent(x));
Mock a Type

If you need to create a type over an abstract class or interface you may use the mocking system of Rystem. For example, if you have an abstract class like this one down below.

public abstract class Alzio
{
    private protected string X { get; }
    public string O => X;
    public string A { get; set; }
    public Alzio(string x)
    {
        X = x;
    }
}

you can create an instace of it or simply mock it with

var mocked = typeof(Alzio).CreateInstance("AAA") as Alzio;
mocked.A = "rrrr";

and you can use the class like a real class. You also may do it with

Alzio alzio = null!;
var mocked = alzio.CreateInstance("AAA");
mocked.A = "rrrr";

or

Mocking.CreateInstance<Alzio>("AAA");

you may see "AAA" as argument for your constructor in abstract class.

Check nullability for properties, fields and parameters.

Following an example from unit test.

private sealed class InModel
{
    public string? A { get; set; }
    public string B { get; set; }
    public string? C;
    public string D;
    public InModel(string? b, string c)
    {
        A = b;
        B = c;
    }
    public void SetSomething(string? b, string c)
    {
        A = b;
        B = c;
    }
}
[Fact]
public void Test1()
{
    var type = typeof(InModel);
    var constructorParameters = type.GetConstructors().First().GetParameters().ToList();
    Assert.True(constructorParameters[0].IsNullable());
    Assert.False(constructorParameters[1].IsNullable());
    var methodParameters = type.GetMethod(nameof(InModel.SetSomething)).GetParameters().ToList();
    Assert.True(methodParameters[0].IsNullable());
    Assert.False(methodParameters[1].IsNullable());
    var properties = type.GetProperties().ToList();
    Assert.True(properties[0].IsNullable());
    Assert.False(properties[1].IsNullable());
    var fields = type.GetFields().ToList();
    Assert.True(fields[0].IsNullable());
    Assert.False(fields[1].IsNullable());
}

Text extensions

You may convert as fast as possible byte[] to string or stream to byte[] or byte[] to stream or stream to string or string to stream. For example, string to byte array and viceversa.

string olfa = "daskemnlandxioasndslam dasmdpoasmdnasndaslkdmlasmv asmdsa";
var bytes = olfa.ToByteArray();
string value = bytes.ConvertToString();

For example, string to stream and viceversa.

string olfa = "daskemnlandxioasndslam dasmdpoasmdnasndaslkdmlasmv asmdsa";
var stream = olfa.ToStream();
string value = stream.ConvertToString();

You may read a string with break lines as an enumerable of string

string olfa = "daskemnlandxioasndslam\ndasmdpoasmdnasndaslkdmlasmv\nasmdsa";
var stream = olfa.ToStream();
var strings = new List<string>();
await foreach (var x in stream.ReadLinesAsync())
{
    strings.Add(x);
}

A simple method to make uppercase the first character.

string olfa = "dasda";
var olfa2 = olfa.ToUpperCaseFirst();

A simple method to check if a char is contained at least X times.

string value = "abcderfa";
bool containsAtLeastTwoAChar = value.ContainsAtLeast(2, 'a');

Character separated-value (CSV)

Transform any kind of IEnumerable data in a CSV string.

string value = _models.ToCsv();

Minimization of a model (based on CSV concept)

It's a brand new idea to serialize any kind of objects (with lesser occupied space of json), the idea comes from Command separated-value standard. To serialize

string value = _models.ToMinimize();

To deserialize (for instance in a List of a class named CsvModel)

value.FromMinimization<List<CsvModel>>();

Extensions for json

I don't know if you are fed up to write JsonSerializer.Serialize, I do, and so, you may use the extension method to serialize faster. To serialize

var text = value.ToJson();

To deserialize in a class (for instance a class named User)

var value = text.FromJson<User>();

Extensions for Task

I don't know if you still are fed up to write .ConfigureAwait(false) to eliminate the context waiting for a task. I do. Why should I set the configure await to false? To set configure await to false

await {your async task}.NoContext();

Instead, to get the result as synchronous result but with a configure await set to false.

{your async task}.ToResult();

You may change the behavior of your NoContext() or ToResult(), setting (in the bootstrap of your application for example)

RystemTask.WaitYourStartingThread = true;

When do I need a true? In windows application for example you have to return after a button clicked to the same thread that started the request.

TaskManager

When you need to run a list of tasks concurrently you may use this static method.

In the next example with TaskManager.WhenAll you may run a method ExecuteAsync {times} times with {concurrentTasks} times in concurrency, and running them when a time slot is free. For example if you run this function with 8 times and 3 concurrentsTasks and true in runEverytimeASlotIsFree You will have this behavior: first 3 tasks starts and since the fourth the implementation waits the end of one of the 3 started before. As soon as one of the 3 started is finished the implementation starts to run the fourth.

var bag = new ConcurrentBag<int>();
await TaskManager.WhenAll(ExecuteAsync, times, concurrentTasks, runEverytimeASlotIsFree).NoContext();

Assert.Equal(times, bag.Count);

async Task ExecuteAsync(int i, CancellationToken cancellationToken)
{
    await Task.Delay(i * 20, cancellationToken).NoContext();
    bag.Add(i);
}

You may run a {atLeast} times of tasks and stopping to wait the remaining tasks with TaskManager.WhenAtLeast

var bag = new ConcurrentBag<int>();
await TaskManager.WhenAtLeast(ExecuteAsync, times, atLeast, concurrentTasks).NoContext();

Assert.True(bag.Count < times);
Assert.True(bag.Count >= atLeast);

async Task ExecuteAsync(int i, CancellationToken cancellationToken)
{
    await Task.Delay(i * 20, cancellationToken).NoContext();
    bag.Add(i);
}

Concurrency

ConcurrentList

You can use the ConcurrentList implementation to have the List behavior with lock operations.

var items = new ConcurrentList<ItemClass>();
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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 Rystem:

Package Downloads
Rystem.DependencyInjection

Rystem is a open-source framework to improve the System namespace in .Net

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.4 164 4/3/2024
6.0.3 391 3/25/2024
6.0.2 288,126 3/11/2024
6.0.1 51,006 3/8/2024
6.0.0 1,171,206 11/21/2023
6.0.0-rc.6 165 10/25/2023
6.0.0-rc.5 118 10/25/2023
6.0.0-rc.4 88 10/23/2023
6.0.0-rc.3 88 10/19/2023
6.0.0-rc.2 157 10/18/2023
6.0.0-rc.1 183 10/16/2023
5.0.20 640,405 9/25/2023
5.0.19 2,636 9/10/2023
5.0.18 1,940 9/6/2023
5.0.17 1,782 9/6/2023
5.0.16 1,842 9/5/2023
5.0.15 1,814 9/5/2023
5.0.14 1,806 9/5/2023
5.0.13 1,920 9/1/2023
5.0.12 1,761 8/31/2023
5.0.11 1,755 8/30/2023
5.0.10 1,776 8/29/2023
5.0.9 1,827 8/24/2023
5.0.8 1,841 8/24/2023
5.0.7 451,319 8/23/2023
5.0.6 19,360 8/21/2023
5.0.5 6,040 8/21/2023
5.0.4 1,885 8/16/2023
5.0.3 214,609 8/2/2023
5.0.2 3,646 8/2/2023
5.0.1 13,459 8/1/2023
5.0.0 13,769 7/31/2023
4.1.26 142,853 7/20/2023
4.1.25 24,651 7/16/2023
4.1.24 401,708 6/13/2023
4.1.23 47,561 6/13/2023
4.1.22 131,391 5/30/2023
4.1.21 57,655 5/20/2023
4.1.20 406,918 4/19/2023
4.1.19 97,615 3/20/2023
4.1.18 2,264 3/20/2023
4.1.17 2,542 3/16/2023
4.1.16 2,313 3/16/2023
4.1.15 2,715 3/15/2023
4.1.14 12,105 3/9/2023
4.1.13 2,409 3/7/2023
4.1.12 2,755 2/9/2023
4.1.11 2,443 1/26/2023
4.1.10 2,645 1/22/2023
4.1.9 2,286 1/20/2023
4.1.8 2,477 1/18/2023
4.1.7 2,524 1/18/2023
4.1.6 2,390 1/17/2023
4.1.5 331 1/15/2023
4.1.4 329 1/15/2023
4.1.3 327 1/15/2023
4.1.2 344 1/15/2023
4.1.1 2,380 1/4/2023
4.1.0 2,550 12/30/2022
4.0.102 2,579 12/21/2022
4.0.101 726 12/20/2022
4.0.100 317 12/19/2022
4.0.99 343 12/18/2022
4.0.98 5,909 12/7/2022
4.0.97 328 12/7/2022
4.0.96 324 12/7/2022
4.0.95 326 12/7/2022
4.0.94 3,998 12/1/2022
4.0.93 358 12/1/2022
4.0.92 3,042 11/29/2022
4.0.91 7,191 11/22/2022
4.0.90 4,295 11/18/2022
4.0.89 4,032 11/18/2022
4.0.88 4,176 11/15/2022
4.0.87 4,118 11/14/2022
4.0.86 4,248 11/13/2022
4.0.85 4,566 11/2/2022
4.0.84 4,248 11/2/2022
4.0.83 4,421 10/29/2022
4.0.82 4,504 10/29/2022
4.0.81 1,608 10/29/2022
4.0.80 13,360 10/16/2022
4.0.79 4,072 10/9/2022
4.0.78 428 10/9/2022
4.0.77 416 10/8/2022
4.0.76 20,364 10/5/2022
4.0.75 41,470 9/20/2022
4.0.74 4,626 9/20/2022
4.0.73 4,236 9/20/2022
4.0.72 643 9/20/2022
4.0.71 3,741 9/20/2022
4.0.70 518 9/14/2022
4.0.69 486 9/14/2022
4.0.68 4,476 9/13/2022
4.0.67 460 9/1/2022
4.0.66 4,232 8/19/2022
4.0.65 439 8/19/2022
4.0.64 451 8/19/2022
4.0.63 474 8/16/2022
4.0.62 486 8/16/2022
4.0.61 431 8/16/2022
4.0.60 458 8/14/2022
4.0.59 483 8/13/2022
4.0.58 455 8/13/2022
4.0.57 477 8/13/2022
4.0.56 461 8/13/2022
4.0.54 486 8/13/2022
4.0.53 477 8/12/2022
4.0.52 447 8/11/2022
4.0.51 472 8/9/2022
4.0.50 475 8/8/2022
4.0.49 465 8/8/2022
4.0.48 460 8/8/2022
4.0.47 461 8/6/2022
4.0.46 449 8/6/2022
4.0.45 453 8/5/2022
4.0.44 441 8/5/2022
4.0.43 467 8/4/2022
4.0.42 473 8/4/2022
4.0.41 470 8/4/2022
4.0.40 458 8/3/2022
4.0.39 466 8/3/2022
4.0.38 4,574 7/30/2022
4.0.37 4,313 7/29/2022
4.0.36 3,804 7/29/2022
4.0.35 464 7/29/2022
4.0.34 761 7/29/2022
4.0.33 4,502 7/29/2022
4.0.32 4,305 7/29/2022
4.0.31 472 7/29/2022
4.0.30 476 7/29/2022
4.0.29 4,358 7/27/2022
4.0.28 517 7/27/2022
4.0.26 4,291 7/27/2022
4.0.25 3,930 7/26/2022
4.0.24 4,409 7/25/2022
4.0.23 4,333 7/25/2022
4.0.22 4,223 7/22/2022
4.0.21 4,284 7/19/2022
4.0.20 4,307 7/19/2022
4.0.19 487 7/19/2022
4.0.18 4,320 7/19/2022
4.0.17 4,351 7/19/2022
4.0.16 4,387 7/18/2022
4.0.15 4,266 7/18/2022
4.0.14 495 7/18/2022
4.0.13 4,313 7/18/2022
4.0.12 3,855 7/17/2022
4.0.11 1,413 7/17/2022
4.0.10 498 7/17/2022
4.0.9 1,114 7/17/2022
4.0.8 13,083 7/15/2022
4.0.7 5,460 7/15/2022
4.0.6 6,160 7/8/2022
4.0.5 5,876 7/6/2022
4.0.4 10,833 7/1/2022
4.0.3 1,269 6/29/2022
4.0.1 1,731 6/29/2022
4.0.0 1,230 6/27/2022
3.0.2 1,135 5/5/2022
3.0.1 1,414 11/30/2021
3.0.0 1,372 11/12/2021
2.0.12 2,429 10/14/2021
2.0.11 1,512 10/14/2021
2.0.8 2,314 10/6/2021
2.0.7 1,699 10/2/2021
2.0.6 443 9/30/2021
2.0.5 402 9/30/2021
2.0.4 1,512 9/29/2021
2.0.3 1,617 9/28/2021
2.0.2 1,324 9/26/2021
2.0.1 1,201 9/24/2021
2.0.0 1,279 9/22/2021
1.4.0 1,363 9/14/2021
1.3.18 1,274 9/10/2021
1.3.17 1,091 9/9/2021
1.3.16 1,400 8/28/2021
1.3.15 1,164 8/26/2021
1.3.14 1,140 8/19/2021
1.3.13 1,150 8/13/2021
1.3.12 1,202 8/12/2021
1.3.11 473 7/29/2021
1.3.10 1,258 7/27/2021
1.3.9 1,204 7/27/2021
1.3.1 1,278 7/25/2021
1.3.0 1,937 7/17/2021
1.2.4 1,221 7/14/2021
1.2.3 423 7/14/2021
1.2.2 402 7/14/2021
1.2.1 1,273 7/14/2021
1.2.0 2,526 7/13/2021
1.1.0 625 4/14/2021
1.0.3 383 4/13/2021
1.0.2 393 4/13/2021
1.0.1 387 4/13/2021
1.0.0 410 4/12/2021