Class GeneralExtensions
- Namespace
- DotNetExtensionKit
- Assembly
- DotNetExtensionKit.dll
Provides general-purpose utility extension methods including null checks, safe casting, conditional execution, transformations, and reflection helpers.
public static class GeneralExtensions
- Inheritance
-
GeneralExtensions
- Inherited Members
Methods
Also<T>(T, Action<T>)
Executes the specified action on the value and returns the original value unchanged.
public static T Also<T>(this T value, Action<T> action)
Parameters
valueTThe value to pass to the action.
actionAction<T>The action to execute on the value.
Returns
- T
The original
value, unchanged.
Type Parameters
TThe type of the value.
Examples
var list = new List<int> { 1, 2, 3 }
.Also(l => Console.WriteLine(l.Count)); // prints 3, returns the list
As<T>(object?)
Performs a safe cast, returning null if the value is not compatible with T.
public static T? As<T>(this object? value) where T : class
Parameters
valueobjectThe object to cast.
Returns
- T
The value cast to
T, ornullif the cast is not valid.
Type Parameters
TThe target reference type to cast to.
Examples
object obj = "hello";
string? text = obj.As<string>(); // "hello"
CallGenericMethod(object, string, Type[], params object[])
Locates an open generic method by name and arity on the instance's runtime type, closes it with the provided type arguments, invokes it, and returns the result.
public static object CallGenericMethod(this object instance, string methodName, Type[] typeArguments, params object[] parameters)
Parameters
instanceobjectThe object instance on which to invoke the method.
methodNamestringThe name of the generic method to invoke.
typeArgumentsType[]The type arguments to close the generic method with.
parametersobject[]The parameters to pass to the method.
Returns
- object
The return value of the invoked method.
Examples
var result = myInstance.CallGenericMethod(
"MyMethod", new[] { typeof(string) }, "hello");
Exceptions
- InvalidOperationException
Thrown when a method with the specified name and generic arity is not found on the instance's type.
CallGenericMethod(Type, string, Type[], object, params object[])
Locates an open generic method by name and arity on the specified type, closes it with the provided type arguments, invokes it, and returns the result.
public static object CallGenericMethod(this Type type, string methodName, Type[] typeArguments, object instance, params object[] parameters)
Parameters
typeTypeThe type on which to search for the generic method.
methodNamestringThe name of the generic method to invoke.
typeArgumentsType[]The type arguments to close the generic method with.
instanceobjectThe object instance on which to invoke the method, or
nullfor static methods.parametersobject[]The parameters to pass to the method.
Returns
- object
The return value of the invoked method.
Examples
var result = typeof(MyClass).CallGenericMethod(
"MyMethod", new[] { typeof(int) }, myInstance, 42);
Exceptions
- InvalidOperationException
Thrown when a method with the specified name and generic arity is not found on the type.
IfNot<T>(T, bool)
Returns the value when the condition is false, or default(T) otherwise.
public static T IfNot<T>(this T value, bool condition)
Parameters
valueTThe value to return when the condition is not met.
conditionboolThe condition to evaluate.
Returns
- T
The original
valueifconditionisfalse; otherwise,default(T).
Type Parameters
TThe type of the value.
Examples
int score = 100;
int result = score.IfNot(score > 200); // 100
If<T>(T, bool)
Returns the value when the condition is true, or default(T) otherwise.
public static T If<T>(this T value, bool condition)
Parameters
valueTThe value to return when the condition is met.
conditionboolThe condition to evaluate.
Returns
- T
The original
valueifconditionistrue; otherwise,default(T).
Type Parameters
TThe type of the value.
Examples
int score = 100;
int result = score.If(score > 50); // 100
IsBetween<T>(T, T, T)
Returns true if the value is within the inclusive range [min, max].
public static bool IsBetween<T>(this T value, T min, T max) where T : IComparable<T>
Parameters
valueTThe value to check.
minTThe inclusive lower bound.
maxTThe inclusive upper bound.
Returns
- bool
trueifvalueis betweenminandmaxinclusive; otherwise,false.
Type Parameters
TThe comparable type of the value.
Examples
bool inRange = 5.IsBetween(1, 10); // true
IsIn<T>(T, params T[])
Returns true if the value equals any element in the specified set.
public static bool IsIn<T>(this T value, params T[] values)
Parameters
valueTThe value to search for.
valuesT[]The set of values to search within.
Returns
- bool
trueifvalueis found invalues; otherwise,false.
Type Parameters
TThe type of the value and set elements.
Examples
bool found = "b".IsIn("a", "b", "c"); // true
IsNotNull<T>(T?)
Returns true if the value is not null.
public static bool IsNotNull<T>(this T? value) where T : class
Parameters
valueTThe value to check for non-null.
Returns
- bool
trueifvalueis not null; otherwise,false.
Type Parameters
TThe reference type of the value.
Examples
string name = "Alice";
bool result = name.IsNotNull(); // true
IsNull<T>(T?)
Returns true if the value is null.
public static bool IsNull<T>(this T? value) where T : class
Parameters
valueTThe value to check for null.
Returns
- bool
trueifvalueis null; otherwise,false.
Type Parameters
TThe reference type of the value.
Examples
string? name = null;
bool result = name.IsNull(); // true
Transform<T, TResult>(T, Func<T, TResult>)
Applies the specified function to the value and returns the result.
public static TResult Transform<T, TResult>(this T value, Func<T, TResult> transformer)
Parameters
valueTThe value to transform.
transformerFunc<T, TResult>The function to apply to the value.
Returns
- TResult
The result of applying
transformertovalue.
Type Parameters
TThe type of the input value.
TResultThe type of the result.
Examples
int length = "hello".Transform(s => s.Length); // 5