-
Notifications
You must be signed in to change notification settings - Fork 0
Object Extensions
This utility library provides extension methods for generic and object-related operations, allowing you to perform various checks, type conversions, and formatting on objects.
Checks if the object is null.
// Example usage with an object
object obj = null;
bool isNull = obj.IsNull();
Console.WriteLine($"Is null: {isNull}");
// Output: Is null: TrueParameters:
-
obj(object): The object to check.
Returns:
- bool: True if the object is null, false otherwise.
Checks if the object is not null.
// Example usage with an object
object obj = new object();
bool isNotNull = obj.IsNotNull();
Console.WriteLine($"Is not null: {isNotNull}");
// Output: Is not null: TrueParameters:
-
obj(object): The object to check.
Returns:
- bool: True if the object is not null, false otherwise.
Casts the object to the specified type.
// Example usage with an object
object obj = "123";
int intValue = obj.CastType<int>();
Console.WriteLine($"Int value: {intValue}");
// Output: Int value: 123Parameters:
-
obj(object): The object to cast.
Returns:
- T: The object cast to the specified type, or the default value for the type if the cast fails.
Casts the object to the specified type.
// Example usage with an object
object obj = "123";
Type targetType = typeof(int);
object castedValue = obj.CastType(targetType);
Console.WriteLine($"Casted value: {castedValue}");
// Output: Casted value: 123Parameters:
-
obj(object): The object to cast. -
type(Type): The type to cast to.
Returns:
- object: The object cast to the specified type, or the default value for the type if the cast fails.
Converts the object to the specified type.
// Example usage with an object
object obj = "123";
int intValue = obj.ConvertTo<int>();
Console.WriteLine($"Int value: {intValue}");
// Output: Int value: 123Parameters:
-
obj(object): The object to convert. -
defaultValue(T): The value to return if the conversion fails.
Returns:
- T: The object converted to the specified type, or the default value if the conversion fails.
Checks if the object can be converted to the specified type.
// Example usage with an object
object obj = "123";
bool canConvert = obj.CanConvertTo<int>();
Console.WriteLine($"Can convert to int: {canConvert}");
// Output: Can convert to int: TrueParameters:
-
obj(object): The object to check.
Returns:
- bool: True if the object can be converted to the specified type, false otherwise.
Converts the object to a string.
// Example usage with an object
object obj = 123;
string stringValue = obj.AsString();
Console.WriteLine($"String value: {stringValue}");
// Output: String value: 123Parameters:
-
obj(object): The object to convert.
Returns:
- string: The object as a string, or null if the object is null.
Converts the object to a string using the specified format provider.
// Example usage with an object
object obj = 123.456;
string stringValue = obj.AsString(CultureInfo.InvariantCulture);
Console.WriteLine($"String value: {stringValue}");
// Output: String value: 123.456Parameters:
-
obj(object): The object to convert. -
formatProvider(IFormatProvider): The format provider to use.
Returns:
- string: The object as a string using the specified format provider.
Converts the object to a string using the invariant culture.
// Example usage with an object
object obj = 123.456;
string stringValue = obj.AsInvariantString();
Console.WriteLine($"String value: {stringValue}");
// Output: String value: 123.456Parameters:
-
obj(object): The object to convert.
Returns:
- string: The object as a string using the invariant culture.
Converts the object to an integer.
// Example usage with an object
object obj = "123";
int intValue = obj.AsInt();
Console.WriteLine($"Int value: {intValue}");
// Output: Int value: 123Parameters:
-
obj(object): The object to convert.
Returns:
- int: The object as an integer, or the default value for integers if the conversion fails.
Converts the object to a long.
// Example usage with an object
object obj = "123";
long longValue = obj.AsLong();
Console.WriteLine($"Long value: {longValue}");
// Output: Long value: 123Parameters:
-
obj(object): The object to convert.
Returns:
- long: The object as a long, or the default value for longs if the conversion fails.
Converts the object to a short.
// Example usage with an object
object obj = "123";
short shortValue = obj.AsShort();
Console.WriteLine($"Short value: {shortValue}");
// Output: Short value: 123Parameters:
-
obj(object): The object to convert.
Returns:
- short: The object as a short, or the default value for shorts if the conversion fails.
Converts the object to a float.
// Example usage with an object
object obj = "123.456";
float floatValue = obj.AsFloat();
Console.WriteLine($"Float value: {floatValue}");
// Output: Float value: 123.456Parameters:
-
obj(object): The object to convert.
Returns:
- float: The object as a float, or the default value for floats if the conversion fails.
Converts the object to a double.
// Example usage with an
object
object obj = "123.456";
double doubleValue = obj.AsDouble();
Console.WriteLine($"Double value: {doubleValue}");
// Output: Double value: 123.456Parameters:
-
obj(object): The object to convert.
Returns:
- double: The object as a double, or the default value for doubles if the conversion fails.
Converts the object to a decimal.
// Example usage with an object
object obj = "123.456";
decimal decimalValue = obj.AsDecimal();
Console.WriteLine($"Decimal value: {decimalValue}");
// Output: Decimal value: 123.456Parameters:
-
obj(object): The object to convert.
Returns:
- decimal: The object as a decimal, or the default value for decimals if the conversion fails.