AbYzzX.System.Extensions
0.0.2
See the version list below for details.
dotnet add package AbYzzX.System.Extensions --version 0.0.2
NuGet\Install-Package AbYzzX.System.Extensions -Version 0.0.2
<PackageReference Include="AbYzzX.System.Extensions" Version="0.0.2" />
<PackageVersion Include="AbYzzX.System.Extensions" Version="0.0.2" />
<PackageReference Include="AbYzzX.System.Extensions" />
paket add AbYzzX.System.Extensions --version 0.0.2
#r "nuget: AbYzzX.System.Extensions, 0.0.2"
#:package AbYzzX.System.Extensions@0.0.2
#addin nuget:?package=AbYzzX.System.Extensions&version=0.0.2
#tool nuget:?package=AbYzzX.System.Extensions&version=0.0.2
System.Extensions
A collection of useful extension methods and utility classes for .NET applications that enhance the standard System namespace with commonly needed functionality.
Overview
System.Extensions provides a set of carefully crafted extension methods and helper classes that simplify common programming tasks in .NET. The library focuses on argument validation, type operations, and collection manipulation.
Features
- Argument Validation: Comprehensive
Checkutility class for parameter validation - Type Extensions: Enhanced type checking and reflection utilities
- Collection Extensions: Useful extension methods for working with collections
- Enumerable Extensions: Recursive flattening and tree traversal support
- Dictionary Extensions: Additional dictionary manipulation methods
Installation
dotnet add package System.Extensions
Components
Check Class
A robust argument validation utility with a fluent API that helps you write defensive code with minimal boilerplate.
Null Checking
public void ProcessUser(User user, string name)
{
Check.NotNull(user, nameof(user));
Check.NotNull(name, nameof(name));
// Your logic here
}
String Validation
public void CreateUser(string username, string email)
{
// Validate non-null or whitespace
Check.NotNullOrWhiteSpace(username, nameof(username));
// Validate with length constraints
Check.NotNullOrEmpty(email, nameof(email), maxLength: 100, minLength: 5);
// Length validation only
Check.Length(email, nameof(email), maxLength: 100, minLength: 5);
}
Collection Validation
public void ProcessItems(ICollection<string> items)
{
Check.NotNullOrEmpty(items, nameof(items));
// items is guaranteed to be non-null and contain at least one element
}
Numeric Range Validation
public void SetAge(int age)
{
Check.Positive(age, nameof(age));
Check.Range(age, nameof(age), minimumValue: 0, maximumValue: 150);
}
public void SetPrice(decimal price)
{
Check.Positive(price, nameof(price));
}
Type Validation
public void RegisterService(Type serviceType)
{
Check.AssignableTo<IService>(serviceType, nameof(serviceType));
// serviceType is guaranteed to implement IService
}
Struct Validation
public void ProcessDate(DateTime? date)
{
var validDate = Check.NotDefaultOrNull(date, nameof(date));
// validDate is guaranteed to be non-null and not default(DateTime)
}
Type Extensions
Enhanced type checking and reflection utilities.
Type Assignment Checking
Type myType = typeof(MyClass);
// Check if type is assignable to target type
if (myType.IsAssignableTo<IMyInterface>())
{
// Type implements IMyInterface
}
if (myType.IsAssignableTo(typeof(BaseClass)))
{
// Type inherits from BaseClass
}
Generic Type Checking
Type listType = typeof(List<string>);
if (listType.IsAssignableToGenericType(typeof(IEnumerable<>)))
{
// listType implements IEnumerable<T>
}
Base Class Retrieval
Type myType = typeof(MyClass);
// Get all base classes
Type[] baseClasses = myType.GetBaseClasses(includeObject: false);
// Get base classes up to a specific type
Type[] baseClasses = myType.GetBaseClasses(typeof(BaseClass), includeObject: false);
Type Name Utilities
Type myType = typeof(MyClass);
string fullName = myType.GetFullNameWithAssemblyName();
// Returns: "MyNamespace.MyClass, MyAssembly"
Collection Extensions
Additional methods for working with collections.
// Example usage (specific methods depend on implementation)
var collection = new List<int> { 1, 2, 3 };
// Use collection extension methods
Dictionary Extensions
Enhanced dictionary manipulation methods.
// Example usage (specific methods depend on implementation)
var dictionary = new Dictionary<string, int>();
// Use dictionary extension methods
Enumerable Extensions
Powerful enumerable extensions for working with hierarchical data.
Flatten Method
Recursively flattens a tree-like structure into a flat enumerable.
public class TreeNode
{
public string Name { get; set; }
public List<TreeNode> Children { get; set; } = new();
}
var root = new TreeNode
{
Name = "Root",
Children = new List<TreeNode>
{
new() { Name = "Child 1", Children = new() { new() { Name = "Grandchild 1" } } },
new() { Name = "Child 2" }
}
};
// Flatten the tree structure
var allNodes = new[] { root }.Flatten(node => node.Children);
// Result: Root, Child 1, Grandchild 1, Child 2
foreach (var node in allNodes)
{
Console.WriteLine(node.Name);
}
Use Cases for Flatten
// File system traversal
public class FileSystemItem
{
public string Path { get; set; }
public List<FileSystemItem> SubItems { get; set; }
}
var allFiles = rootFolder.Flatten(f => f.SubItems);
// Organization hierarchy
public class Employee
{
public string Name { get; set; }
public List<Employee> DirectReports { get; set; }
}
var allEmployees = ceo.Flatten(e => e.DirectReports);
// UI component tree
public class UIComponent
{
public string Id { get; set; }
public List<UIComponent> Children { get; set; }
}
var allComponents = rootComponent.Flatten(c => c.Children);
Complete Examples
API Controller with Validation
public class UserController
{
public void CreateUser(CreateUserRequest request)
{
Check.NotNull(request, nameof(request));
Check.NotNullOrWhiteSpace(request.Username, nameof(request.Username), maxLength: 50);
Check.NotNullOrWhiteSpace(request.Email, nameof(request.Email), maxLength: 100);
Check.Range(request.Age, nameof(request.Age), minimumValue: 18, maximumValue: 120);
// Create user logic
}
}
Service Registration with Type Checking
public void RegisterService<TService, TImplementation>()
where TImplementation : TService
{
var implementationType = typeof(TImplementation);
var serviceType = typeof(TService);
Check.NotNull(implementationType, nameof(implementationType));
Check.AssignableTo<TService>(implementationType, nameof(implementationType));
if (serviceType.IsAssignableToGenericType(typeof(IDisposable)))
{
// Register with disposal support
}
// Registration logic
}
Hierarchical Data Processing
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public List<Category> SubCategories { get; set; } = new();
}
public IEnumerable<Category> GetAllCategories(Category rootCategory)
{
Check.NotNull(rootCategory, nameof(rootCategory));
return new[] { rootCategory }.Flatten(c => c.SubCategories);
}
public Category? FindCategory(Category root, int categoryId)
{
return new[] { root }
.Flatten(c => c.SubCategories)
.FirstOrDefault(c => c.Id == categoryId);
}
Best Practices
Use Check for All Public Methods
public class MyService
{
public void ProcessData(string data, IConfiguration config)
{
// Always validate public method parameters
Check.NotNullOrWhiteSpace(data, nameof(data));
Check.NotNull(config, nameof(config));
// Your logic here
}
private void InternalMethod(string data)
{
// Internal methods may skip validation if callers are trusted
}
}
Combine Multiple Validations
public void CreateProduct(string name, decimal price, int stock)
{
Check.NotNullOrWhiteSpace(name, nameof(name), maxLength: 100, minLength: 3);
Check.Positive(price, nameof(price));
Check.Range(stock, nameof(stock), minimumValue: 0);
// Create product
}
Use Type Extensions for Generic Code
public void ProcessGenericType(Type type)
{
Check.NotNull(type, nameof(type));
if (type.IsAssignableTo<IDisposable>())
{
// Handle disposable types
}
if (type.IsAssignableToGenericType(typeof(IEnumerable<>)))
{
// Handle enumerable types
}
}
Requirements
- .NET 9.0 or later
- C# 13.0
Dependencies
This package has no external dependencies and only extends the System namespace.
License
This project is licensed under the terms specified in the project license file.
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
Built with ❤️ for the .NET community
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on AbYzzX.System.Extensions:
| Package | Downloads |
|---|---|
|
AbYzzX.Avalonia.Modular
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.