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

前端 未结 4 1241
醉酒成梦
醉酒成梦 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:00

    Because, looking at the code via Reflector, Cast doesnt attempt to take any implicit cast operators (the LINQ Cast code is heavily optimised for special cases of all kinds, but nothing in that direction) into account (as many .NET languages won't).

    Without getting into reflection and other things, generics doesnt offer any out of the box way to take such extra stuff into account in any case.

    EDIT: In general, more complex facilities like implicit/explict, equality operators etc. are not generally handled by generic facilities like LINQ.

提交回复
热议问题