public static Object Cast(this Type to, Object instance)
{
var from = instance.GetType();
if (!to.IsAssignableFrom(from))
{
var flags = BindingFlags.Public | BindingFlags.Static;
var method = to.GetMethods(flags).Union(from.GetMethods(flags))
.Where(m => m.ReturnType == to && (m.Name == "op_Implicit" || m.Name == "op_Explicit") && m.GetParameters()[0].ParameterType == from).First();
return method.Invoke(null, new[] { instance });
}
return instance;
}