GithubHelp home page GithubHelp logo

lanicon / typeconverter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thomasgalliker/typeconverter

0.0 1.0 0.0 4.14 MB

TypeConverter is a lightweight, portable class library which allows to convert between objects of different types.

PowerShell 11.51% C# 88.49%

typeconverter's Introduction

TypeConverter

TypeConverter

TypeConverter is a lightweight, portable class library which allows to convert between objects of different types. The philosophy behind TypeConverter is that type conversion should no longer be a painfull topic to developers. As a developer, you simply specify source and target type and pass in your desired object which you want to convert.

This library is shipped with some basic sample conversion strategies, however, you are free to write your own type converters and register them in the IConverterRegistry. The most important type conversions provided by the .Net framework are integrated into TypeConverter. However, your own converters are always preferred over the .Net integrated default converstion/casting strategy. Following order of priority is respected:

  • Attempt 1: Try to convert using registered, user-defined IConverters
  • Attempt 2: Return source value if source and target type are the same
  • Attempt 3: Try to cast implicitly to the target type
  • Attempt 4: Try to cast explicitly to the target type
  • Attempt 5: Try to convert between string and enum value if either source or target type is an enum resp. a string
  • Attempt 6: Try to use String.Parse if either source or target type is a string

If all attempts fail, the Convert method throws a ConversionNotSupportedException with the specified reason. TryConvert does return null if no conversion could be done.

Download and Install TypeConverter

This library is available on NuGet: https://www.nuget.org/packages/TypeConverter/ Use the following command to install TypeConverter using NuGet package manager console:

PM> Install-Package TypeConverter

You can use this library in any .Net project which is compatible to PCL (e.g. Xamarin Android, iOS, Windows Phone, Windows Store, Universal Apps, etc.)

API Usage

Create your own type converter

If you want to implement a type converter, you simply implement the IConverter<TFrom, TTo> interface where TFrom is the generic type from which you want to convert and TTo is the type to which you want to convert to. Following sample code illustrates a converter which converts between string and System.Uri.

public class StringToUriConverter : IConverter<string, Uri>, IConverter<Uri, string>
{
    public Uri Convert(string value)
    {
        return new Uri(value);
    }

    public string Convert(Uri value)
    {
        return value.AbsoluteUri;
    }
}

Register a converter

Create (or retrieve via dependency injection) an instance of ConverterRegistry and register those converters you like to use later on. Beware that you will have to register a converter for each direction you want to convert (if you support two-way conversion). Following example shows how to register the StringToUriConverter to convert between string and Uri and vice versa.

IConverterRegistry converterRegistry = new ConverterRegistry();
converterRegistry.RegisterConverter<string, Uri>(() => new StringToUriConverter());
converterRegistry.RegisterConverter<Uri, string>(() => new StringToUriConverter());

Convert between types

Now, after having set-up a basic converter, we can use IConverterRegistry to convert between object of different types.

Convert from string to System.Uri

var uri = converterRegistry.Convert<string, Uri>("http://github.com/");

Convert from System.Uri to string

var uriAsString = converterRegistry.Convert<Uri, string>(uri);

License

TypeConverter is Copyright © 2018 Thomas Galliker. Free for non-commercial use. For commercial use please contact the author.

typeconverter's People

Contributors

thomasgalliker avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.