NYaul 0.0.28
dotnet add package NYaul --version 0.0.28
NuGet\Install-Package NYaul -Version 0.0.28
<PackageReference Include="NYaul" Version="0.0.28" />
paket add NYaul --version 0.0.28
#r "nuget: NYaul, 0.0.28"
// Install NYaul as a Cake Addin #addin nuget:?package=NYaul&version=0.0.28 // Install NYaul as a Cake Tool #tool nuget:?package=NYaul&version=0.0.28
NYaul - .NET Yet Another Utility Library
NYaul is a .NET utility library designed to simplify common development tasks, providing a collection of helpful extension methods, classes, and utilities. It focuses on I/O operations, reflection, disposable resources, and more. It's designed to be lightweight, efficient, and easy to use.
Features
Fluent File and Directory Operations:
- Simplified copying of files and directories (including deep copies and filtering).
- Easy creation and management of temporary files and directories (with automatic cleanup).
- Extension methods for
FileInfo
andDirectoryInfo
to check for extensions, traverse parent directories, and more. FileEx
andDirectoryEx
static classes for common operations.
Stream Utilities:
ActionStream
: Executes an action on stream disposal.TruncateOnDisposeStream
: Truncates a stream to its current position on disposal.ProxyStream
: A base class for creating custom stream wrappers.
Disposable Helpers:
ActionDisposable
: Executes an action on disposal.AsyncActionDisposable
: Executes an asynchronous action on disposal.- Generic versions for actions taking parameters.
Reflection Utilities:
DefaultActivator
: Efficiently creates instances of types using optimized activators (prioritizingCreateDefault
methods,Default
properties/fields, or parameterless constructors).GenericTypeHelper
: Simplifies working with generic types, including extracting generic arguments from complex type hierarchies.
Enum Type Conversion:
EnumTypeConverter
: Converts strings to enum values, supporting custom aliases defined via theEnumAliasAttribute
.
TimeSpan Extensions:
- Allows direct
await
onTimeSpan
and nullableTimeSpan
for easy asynchronous delays.
- Allows direct
Wait Time Generation:
WaitTimeGenerator
: Generates exponentially increasing wait times for retry logic, with configurable maximum retries, base time, and maximum wait time. Includes a fluent builder.
String Extensions
- Provides
Join
methods as polyfills for older .NET versions
- Provides
File System Abstraction:
IFileProvider
: Interface for abstracting file system operations.DefaultFileProvider
: Default implementation usingSystem.IO
.VirtualFileProvider
: In-memory file system implementation for testing.
Cross-Platform Compatibility:
- Works with .NET 8.0, .NET Framework 4.8, and .NET Standard 2.0.
- Uses polyfills for newer language features when targeting older frameworks.
CI/CD:
- Full CI/CD is setup with GitHub Actions
Installation
NYaul is available on NuGet:
Install-Package NYaul
or
dotnet add package NYaul
Usage Examples
1. Fluent Directory Copy:
using NYaul.IO;
// Copy all .txt and .log files from "sourceDir" to "destDir", including subdirectories.
DirectoryEx.Copy
.From(@"C:\sourceDir")
.To(@"C:\destDir")
.WithFileFilter(file => file.HasExtension(".txt", ".log"))
.WithSubdirectories() // Or .DeepCopy()
.Invoke(); // Or .Copy()
2. Temporary File Handling:
using NYaul.IO;
using (var tempFile = TempFile.CreateWithExtension(".txt"))
{
await tempFile.WriteAllTextAsync("This is some temporary data.");
string content = await tempFile.ReadAllTextAsync();
Console.WriteLine(content);
} // tempFile is automatically deleted here.
3. Asynchronous Delay with TimeSpan:
using System;
// ...
await TimeSpan.FromSeconds(5); // Wait for 5 seconds.
await (TimeSpan?)null; // Completes instantly
4. Default Activator:
using NYaul.Reflection;
public class MyClass
{
public static MyClass CreateDefault() => new MyClass { Value = 42 };
public int Value { get; set; }
}
// ...
var instance = DefaultActivator.CreateDefault<MyClass>();
Console.WriteLine(instance.Value); // Output: 42
// Cached delegate for improved performance on subsequent calls:
var activator = DefaultActivator.CreateDefaultActivator<MyClass>();
var instance2 = activator();
5. Enum Conversion with Aliases:
using System.ComponentModel;
using NYaul.TypeConversion;
[TypeConverter(typeof(EnumTypeConverter<MyEnum>))]
public enum MyEnum
{
[EnumAlias("one", "first")]
One,
[EnumAlias("two", "second")]
Two,
Three
}
// ...
var converter = new EnumTypeConverter<MyEnum>();
MyEnum value1 = (MyEnum)converter.ConvertFrom("first"); // value1 == MyEnum.One
MyEnum value2 = (MyEnum)converter.ConvertFrom("THREE"); // value2 == MyEnum.Three
6. Exponential Backoff:
using NYaul;
var waitTimeGenerator = WaitTimeGenerator.Create
.WithMaxRetries(5)
.WithBaseTime(TimeSpan.FromSeconds(1))
.WithMaxWaitTime(TimeSpan.FromSeconds(30))
.Build();
for (int i = 0; i < 10; i++)
{
TimeSpan? waitTime = waitTimeGenerator.GetWaitTime(i);
if (waitTime == null)
{
Console.WriteLine($"Retry attempt {i}: No more retries.");
break;
}
Console.WriteLine($"Retry attempt {i}: Wait for {waitTime.Value.TotalSeconds} seconds.");
// await waitTime; // In a real scenario, you'd await here.
}
7. Virtual File System (for Testing):
using NYaul.IO.FileProvider;
var virtualFileSystem = new VirtualFileProvider();
virtualFileSystem.AddFile("test.txt", "Hello, world!");
bool exists = virtualFileSystem.FileExists("test.txt"); // true
string content = virtualFileSystem.ReadAllText("test.txt"); // "Hello, world!"
using (var stream = virtualFileSystem.OpenWrite("test2.txt"))
using (var writer = new StreamWriter(stream))
{
writer.WriteLine("Line 1");
writer.WriteLine("Line 2");
}
string content2 = virtualFileSystem.ReadAllText("test2.txt"); // "Line 1\r\nLine 2\r\n"
8. Generic Type Helper
using NYaul.Reflection;
public abstract class Provider<TConfig> where TConfig : ConfigItem { }
public class ConfigItem { }
public class Provider1 : Provider<Config1> { }
public class Config1 : ConfigItem { }
// ...
var result = typeof(Provider1).GetFirstGenericArgumentsOfTypeDefinition(typeof(Provider<>));
// result.GenericArguments[0] == typeof(Config1)
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License - see the LICENSE.txt file for details.
Product | Versions 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 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. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 is compatible. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.8
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.0.28 | 94 | 2/24/2025 |
0.0.27 | 84 | 2/24/2025 |
0.0.26 | 88 | 2/24/2025 |
0.0.25 | 81 | 2/24/2025 |
0.0.24 | 75 | 2/24/2025 |
0.0.23 | 67 | 2/23/2025 |
0.0.22 | 73 | 2/23/2025 |
0.0.21 | 71 | 2/23/2025 |
0.0.20 | 70 | 2/23/2025 |
0.0.19 | 70 | 2/23/2025 |
0.0.18 | 72 | 2/23/2025 |
0.0.15 | 75 | 2/23/2025 |
0.0.2 | 114 | 12/12/2024 |
0.0.1 | 85 | 12/12/2024 |