Skip to content

Instantly share code, notes, and snippets.

@rsafier
Last active April 15, 2019 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rsafier/d6bf646ece994da634001303fae3c738 to your computer and use it in GitHub Desktop.
Save rsafier/d6bf646ece994da634001303fae3c738 to your computer and use it in GitHub Desktop.
ConvertTo breaking change
using System;
using ServiceStack;
using ServiceStack.Text;
public class Parent
{
public int x { get;set;}
}
public class Child : Parent
{
public int y {get;set;}
}
var child = new Child();
child.x = 423;
var asParent = OldMethod<Parent>(child);
Console.WriteLine("child type: " + child.GetType().Name);
Console.WriteLine("asParent type: " + asParent.GetType().Name);
Console.WriteLine("child hash: " + child.GetHashCode());
Console.WriteLine("asParent hash: " + asParent.GetHashCode());
asParent = NewMethod<Parent>(child);
Console.WriteLine();
Console.WriteLine("child type: " + child.GetType().Name);
Console.WriteLine("asParent type: " + asParent.GetType().Name);
Console.WriteLine("child hash: " + child.GetHashCode());
Console.WriteLine("asParent hash: " + asParent.GetHashCode());
private T OldMethod<T>(object from) //Prior to 2/27 Add support for using implicit/explicit casts of Value types in Conve
{
var fromType = from.GetType();
if (fromType == typeof(T))
return (T)from;
return (T)AutoMappingUtils.ConvertTo(from, typeof(T));
}
private T NewMethod<T>(object from) //Post 2/27 Add support for using implicit/explicit casts of Value types in Conve
{
if (from is T t)
return t;
return (T)AutoMappingUtils.ConvertTo(from, typeof(T));
}
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ServiceStack.Text" version="5.5.0" targetFramework="net45" />
<package id="ServiceStack.Client" version="5.5.0" targetFramework="net45" />
<package id="ServiceStack.Interfaces" version="5.5.0" targetFramework="net45" />
</packages>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment