Extension methods for object providing safe casting, type checking, and reflection utilities.
Safe Casting
As (Reference Types)
Safely cast with fluent syntax:
AsValue (Value Types)
Safely unbox value types:
Type Checking with Casting
Is (Reference Types)
Combine type check and cast in one operation:
IsValue (Value Types)
Same pattern for value types:
CastTo (Throwing Cast)
When you’re certain of the type and want a clear exception on failure:
Reflection Helpers
Safe property access without catching exceptions.
HasProperty
Check if a property exists:
TryGetPropertyValue
Get property values safely:
Returns default when:
- Object is null
- Property doesn’t exist
- Property value is null
- Property value cannot be cast to the requested type
Equality and Comparison
EqualsTo
Null-safe equality comparison:
IsOneOf / IsNotOneOf
Check if a value is one of several options:
Null Utilities
IfNotNull (Action)
Execute an action if not null:
Transform if not null, return default otherwise:
IfNotNull with Default
Transform with explicit default:
Examples
Processing Roslyn Symbols
Duck Typing with Reflection
State Machine with IsOneOf
Optional Processing
API Reference
Safe Casting
Reflection
Equality
Null Utilities
The Is<T> and IsValue<T> methods are particularly useful in switch statements or when you need both the type
check and the cast result in one operation.