TypeCaster 0.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package TypeCaster --version 0.1.1                
NuGet\Install-Package TypeCaster -Version 0.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TypeCaster" Version="0.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TypeCaster --version 0.1.1                
#r "nuget: TypeCaster, 0.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TypeCaster as a Cake Addin
#addin nuget:?package=TypeCaster&version=0.1.1

// Install TypeCaster as a Cake Tool
#tool nuget:?package=TypeCaster&version=0.1.1                

Nuget Nuget

GitHub Workflow Status GitHub Workflow Status

TypeCaster

Every now and then developers need to map an entity to another class, with a similar structure, for the sake of decoupling and separation of concerns. To avoid manual mapping, which is boring and error prone, TypeCaster offers a simple and elegant solution to automatically cast on class type into anohter.

Usage

Imagine a class with the following properties

public class TestClassA
{
  public int Id { get; set; }
  public string Name { get; set; }
  public MyEnum Type { get; set; }
}

And another claass with a similar structure, but with a string intead of enum in the Type property

public class TestClassB
{
  public int Id { get; set; }
  public string Name { get; set; }
  public string Type { get; set; }
}

Casting a clas of type ClassA to type ClassB is pretty simple

public void ConvertClassAToClassB()
{
  var classA = new TestClassA
  {
    Id = 1,
    Name = "MyClass",
    Type = MyEnum.TypeA
  };
  
  var classB = CastToClass<TestClassB>.From(classA);
}

The other way around works too

public void ConvertClassBToClassA()
{
  var classB = new TestClassB
  {
    Id = 1,
    Name = "MyClass",
    Type = "TypeA"
  };
  
  var classA = CastToClass<TestClassA>.From(classB);
}

Since TypeCaster relies on the class property names, it can do some simple conversions to preserve the class tructure. When a conversion is not possible, the property is set to its type default value.

The CastTo class

TypeCaster relies on two main classes. The CastToClass which was used in last example, and CastTo class. CastTo is responsible for type conversion of properties, and can be used directly in your code, like in the following example.

//Convert string into enum
var typeString = "TypeA";
var type = CastTo<TestEnum>.From(typeString);

//Convert int into double
var myInt = 1;
var myDouble = CastTo<double>.From(myInt);
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.2 456 6/19/2021
0.1.1 299 6/17/2021
0.1.0 331 6/17/2021