https://github.com/dotnettools/convertex
A capable extension to System.Convert
https://github.com/dotnettools/convertex
conversion dotnet system-convert type-conversion
Last synced: 9 months ago
JSON representation
A capable extension to System.Convert
- Host: GitHub
- URL: https://github.com/dotnettools/convertex
- Owner: dotnettools
- License: gpl-3.0
- Created: 2021-11-09T13:02:12.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-13T23:59:42.000Z (over 3 years ago)
- Last Synced: 2024-12-13T23:47:32.355Z (over 1 year ago)
- Topics: conversion, dotnet, system-convert, type-conversion
- Language: C#
- Homepage:
- Size: 53.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ConvertEx
ConvertEx is a capable extension to `System.Convert`. It does its best to return a value instead of throwing exceptions.
## Features
- Supports `Nullable` types.
- Out-of-box converters for non-primitive types e.g. converts a string or number to `TimeSpan` and vice versa.
- Supports custom converters to convert to and from any type.
- Falls back to `System.Convert`, meaning that everything that `System.Convert` can do, `ConvertEx` does too.
```powershell
Install-Package ConvertEx -Version 1.0.5
```
## Usage Examples
### Change type using default converter
```csharp
var integer = ConvertEx.ChangeType("123", typeof(int)); // integer = 123
var uri = ConvertEx.ChangeType("https://github.com/", typeof(Uri)) // Works like a charm - No InvalidCastException!
```
### Convert nullables
```csharp
int? num = 5;
var dbl = ConvertEx.ChangeType(num); // dbl = 5d
```
### Create a custom converter
```csharp
var converter = new TypeConverter()
.AddDigester()
.AddConverter()
.AddConverter()
.AddConverter()
.AddConverter()
.AddConverter();
```
**Note:** To write your own digesters or converters see source code for `ToStringConverter` and `NullableDigester`.