Why does a Linq Cast operation fail when I have an implicit cast defined?

前端 未结 4 1250
醉酒成梦
醉酒成梦 2020-11-27 06:39

I\'ve created two classes, with one of them having an implicit cast between them:

public class Class1
{
    public int Test1;
}

public class Class2
{
    pu         


        
4条回答
  •  不知归路
    2020-11-27 07:18

    You can also use this to do casting with conversions if needed:

    public static IEnumerable CastAll(this IEnumerable items)
    {
     var p = Expression.Parameter(typeof(TItem), "i");
     var c = Expression.Convert(p, typeof(TDest));
     var ex = Expression.Lambda>(c, p).Compile();
    
     foreach (var item in items)
     {
        yield return ex(item);
     }
    }
    

    From http://adventuresdotnet.blogspot.com/2010/06/better-more-type-safe-alternative-to.html

提交回复
热议问题