An open API service indexing awesome lists of open source software.

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

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.

## NuGet Package

```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`.