To convert between some LINQ to SQL objects and DTOs we have created explicit cast operators on the DTOs. That way we can do the following:
DTOType MyDTO =
If you decompile the Linq assembly you get code resembling the following. The previous answer is correct, ultimately the cast is from 'object' to target-type which will always fail for custom types.
private static IEnumerable CastIterator( IEnumerable source )
{
foreach(object current in source)
{
yield return (TResult)( (object)current );
}
yield break;
}
public static IEnumerable DCast( this IEnumerable source )
{
IEnumerable enumerable = source as IEnumerable;
if(enumerable != null)
{
return enumerable;
}
if(source == null)
{
throw new ArgumentNullException( "source" );
}
return CastIterator( source );
}
TFish