Rystem 6.0.21

dotnet add package Rystem --version 6.0.21
NuGet\Install-Package Rystem -Version 6.0.21
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.21" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rystem --version 6.0.21
#r "nuget: Rystem, 6.0.21"
#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.21

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

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 (2)

Showing the top 2 NuGet packages that depend on Rystem:

Package Downloads
Rystem.DependencyInjection

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

Rystem.Authentication.Social.Blazor

Rystem.Authentication.Social helps you to integrate with new .Net Identity system and social logins.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.21 305 6/18/2024
6.0.20 119,422 6/16/2024
6.0.19 30,576 6/14/2024
6.0.18 302 6/14/2024
6.0.17 288 6/14/2024
6.0.16 50,144 6/10/2024
6.0.15 289 6/9/2024
6.0.14 94,559 5/24/2024
6.0.13 298 5/23/2024
6.0.12 220 5/23/2024
6.0.11 321 5/20/2024
6.0.9 335 5/19/2024
6.0.7 318 5/18/2024
6.0.6 275 5/10/2024
6.0.5 245 5/10/2024
6.0.4 550,231 4/3/2024
6.0.3 772 3/25/2024
6.0.2 379,409 3/11/2024
6.0.1 51,043 3/8/2024
6.0.0 1,171,325 11/21/2023
6.0.0-rc.6 168 10/25/2023
6.0.0-rc.5 120 10/25/2023
6.0.0-rc.4 90 10/23/2023
6.0.0-rc.3 90 10/19/2023
6.0.0-rc.2 159 10/18/2023
6.0.0-rc.1 185 10/16/2023
5.0.20 640,552 9/25/2023
5.0.19 2,766 9/10/2023
5.0.18 2,063 9/6/2023
5.0.17 1,899 9/6/2023
5.0.16 1,956 9/5/2023
5.0.15 1,929 9/5/2023
5.0.14 1,926 9/5/2023
5.0.13 2,032 9/1/2023
5.0.12 1,890 8/31/2023
5.0.11 1,877 8/30/2023
5.0.10 1,908 8/29/2023
5.0.9 1,943 8/24/2023
5.0.8 1,957 8/24/2023
5.0.7 451,427 8/23/2023
5.0.6 19,476 8/21/2023
5.0.5 6,152 8/21/2023
5.0.4 2,017 8/16/2023
5.0.3 214,729 8/2/2023
5.0.2 3,766 8/2/2023
5.0.1 13,579 8/1/2023
5.0.0 13,884 7/31/2023
4.1.26 143,022 7/20/2023
4.1.25 24,656 7/16/2023
4.1.24 401,919 6/13/2023
4.1.23 47,677 6/13/2023
4.1.22 131,598 5/30/2023
4.1.21 57,769 5/20/2023
4.1.20 407,035 4/19/2023
4.1.19 97,730 3/20/2023
4.1.18 2,372 3/20/2023
4.1.17 2,663 3/16/2023
4.1.16 2,429 3/16/2023
4.1.15 2,827 3/15/2023
4.1.14 12,228 3/9/2023
4.1.13 2,520 3/7/2023
4.1.12 2,895 2/9/2023
4.1.11 2,556 1/26/2023
4.1.10 2,786 1/22/2023
4.1.9 2,402 1/20/2023
4.1.8 2,597 1/18/2023
4.1.7 2,639 1/18/2023
4.1.6 2,504 1/17/2023
4.1.5 339 1/15/2023
4.1.4 337 1/15/2023
4.1.3 335 1/15/2023
4.1.2 350 1/15/2023
4.1.1 2,502 1/4/2023
4.1.0 2,679 12/30/2022
4.0.102 2,688 12/21/2022
4.0.101 761 12/20/2022
4.0.100 323 12/19/2022
4.0.99 349 12/18/2022
4.0.98 6,219 12/7/2022
4.0.97 340 12/7/2022
4.0.96 330 12/7/2022
4.0.95 330 12/7/2022
4.0.94 4,201 12/1/2022
4.0.93 367 12/1/2022
4.0.92 3,211 11/29/2022
4.0.91 8,004 11/22/2022
4.0.90 4,445 11/18/2022
4.0.89 4,171 11/18/2022
4.0.88 4,315 11/15/2022
4.0.87 4,270 11/14/2022
4.0.86 4,387 11/13/2022
4.0.85 4,704 11/2/2022
4.0.84 4,383 11/2/2022
4.0.83 4,560 10/29/2022
4.0.82 4,644 10/29/2022
4.0.81 1,663 10/29/2022
4.0.80 13,780 10/16/2022
4.0.79 4,198 10/9/2022
4.0.78 432 10/9/2022
4.0.77 420 10/8/2022
4.0.76 21,056 10/5/2022
4.0.75 42,865 9/20/2022
4.0.74 4,748 9/20/2022
4.0.73 4,388 9/20/2022
4.0.72 645 9/20/2022
4.0.71 3,877 9/20/2022
4.0.70 524 9/14/2022
4.0.69 492 9/14/2022
4.0.68 4,611 9/13/2022
4.0.67 462 9/1/2022
4.0.66 4,366 8/19/2022
4.0.65 441 8/19/2022
4.0.64 453 8/19/2022
4.0.63 476 8/16/2022
4.0.62 488 8/16/2022
4.0.61 433 8/16/2022
4.0.60 460 8/14/2022
4.0.59 485 8/13/2022
4.0.58 457 8/13/2022
4.0.57 479 8/13/2022
4.0.56 463 8/13/2022
4.0.54 488 8/13/2022
4.0.53 479 8/12/2022
4.0.52 449 8/11/2022
4.0.51 474 8/9/2022
4.0.50 477 8/8/2022
4.0.49 467 8/8/2022
4.0.48 462 8/8/2022
4.0.47 469 8/6/2022
4.0.46 451 8/6/2022
4.0.45 455 8/5/2022
4.0.44 443 8/5/2022
4.0.43 469 8/4/2022
4.0.42 475 8/4/2022
4.0.41 472 8/4/2022
4.0.40 460 8/3/2022
4.0.39 468 8/3/2022
4.0.38 4,704 7/30/2022
4.0.37 4,443 7/29/2022
4.0.36 3,921 7/29/2022
4.0.35 466 7/29/2022
4.0.34 763 7/29/2022
4.0.33 4,632 7/29/2022
4.0.32 4,448 7/29/2022
4.0.31 474 7/29/2022
4.0.30 478 7/29/2022
4.0.29 4,500 7/27/2022
4.0.28 519 7/27/2022
4.0.26 4,416 7/27/2022
4.0.25 4,065 7/26/2022
4.0.24 4,544 7/25/2022
4.0.23 4,470 7/25/2022
4.0.22 4,360 7/22/2022
4.0.21 4,399 7/19/2022
4.0.20 4,441 7/19/2022
4.0.19 489 7/19/2022
4.0.18 4,464 7/19/2022
4.0.17 4,485 7/19/2022
4.0.16 4,537 7/18/2022
4.0.15 4,403 7/18/2022
4.0.14 497 7/18/2022
4.0.13 4,454 7/18/2022
4.0.12 3,983 7/17/2022
4.0.11 1,446 7/17/2022
4.0.10 500 7/17/2022
4.0.9 1,123 7/17/2022
4.0.8 13,502 7/15/2022
4.0.7 5,651 7/15/2022
4.0.6 6,349 7/8/2022
4.0.5 6,073 7/6/2022
4.0.4 11,179 7/1/2022
4.0.3 1,280 6/29/2022
4.0.1 1,765 6/29/2022
4.0.0 1,238 6/27/2022
3.0.2 1,140 5/5/2022
3.0.1 1,419 11/30/2021
3.0.0 1,374 11/12/2021
2.0.12 2,435 10/14/2021
2.0.11 1,514 10/14/2021
2.0.8 2,316 10/6/2021
2.0.7 1,701 10/2/2021
2.0.6 446 9/30/2021
2.0.5 404 9/30/2021
2.0.4 1,514 9/29/2021
2.0.3 1,619 9/28/2021
2.0.2 1,326 9/26/2021
2.0.1 1,203 9/24/2021
2.0.0 1,281 9/22/2021
1.4.0 1,366 9/14/2021
1.3.18 1,277 9/10/2021
1.3.17 1,093 9/9/2021
1.3.16 1,403 8/28/2021
1.3.15 1,166 8/26/2021
1.3.14 1,142 8/19/2021
1.3.13 1,152 8/13/2021
1.3.12 1,204 8/12/2021
1.3.11 476 7/29/2021
1.3.10 1,260 7/27/2021
1.3.9 1,206 7/27/2021
1.3.1 1,280 7/25/2021
1.3.0 1,939 7/17/2021
1.2.4 1,224 7/14/2021
1.2.3 425 7/14/2021
1.2.2 404 7/14/2021
1.2.1 1,275 7/14/2021
1.2.0 2,530 7/13/2021
1.1.0 628 4/14/2021
1.0.3 386 4/13/2021
1.0.2 395 4/13/2021
1.0.1 389 4/13/2021
1.0.0 413 4/12/2021