简体   繁体   English

如何隐式转换列表 <Foo> 列出 <Bar> 鉴于定义了强制转换运算符?

[英]How can I implicitly convert List<Foo> to List<Bar> given that there's a cast operator defined?

I have a type Bar that defines an implicit cast operator: 我有一个Bar类型,它定义了一个隐式强制转换运算符:

public static implicit operator Bar(Foo foo)
{
    return new Bar(foo.property);
}

Converting single objects works great, but now I have a List<Foo> and I need a List<Bar> . 转换单个对象的效果很好,但是现在我有了一个List<Foo>并且我需要一个List<Bar>

I found this workaround: 我发现此解决方法:

List<Bar> barList = fooList.ConvertAll<Bar>(item => item);

which seems to do the trick, but it's somewhat defeating the point of an implicit cast operator if I have to apply it in this somewhat explicit sense. 这似乎可以解决问题,但是如果我必须在某种显式意义上应用它,那么它在某种程度上克服了隐式强制转换运算符的观点。 Is there any way to make the implicit conversion work for lists? 有什么方法可以使隐式转换适用于列表?

Unfortunately, you cannot make this cast implicit, although the reason is somewhat unexpected: when you explicitly provide a type argument to a generic method, C# requires you to provide all remaining arguments as well. 不幸的是,尽管原因有些出乎意料,但是您不能使此强制转换隐式:当您为通用方法显式提供类型实参时,C#还要求您提供所有剩余的实参。 It is this all-or-nothing approach that lets you write this 正是这种全有或全无的方法可以让您编写此内容

List<Bar> barList = fooList.Select(item => (Bar)item).ToList();

or this 或这个

List<Bar> barList = fooList.Select<Foo,Bar>(item => item);

but it does not let you write this: 但是它不允许您这样写:

List<Bar> barList = fooList.Select<...,Bar>(item => item);
//                                 ^^^
//                                  |
// Figure this out from the context |

Theoretically, C# could capture Foo from the type of item , but it does not have a feature to do it. 从理论上讲,C# 可以item的类型中捕获Foo ,但是它没有执行此操作的功能。

LINQ's Cast<T> is not going to work either, because it treats items being cast as plain object s, thus ignoring any conversion operators that may be defined on them. LINQ的Cast<T>也不起作用,因为它会将要转换的项目视为普通object ,从而忽略了可能在其上定义的任何转换运算符。

Since you are required to specify both the "from" and the "to" types, a solution based on generics is not going to be any better than your solution based on ConvertAll . 由于需要同时指定“ from”和“ to”类型,因此基于泛型的解决方案不会比基于ConvertAll的解决方案更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何投放清单 <Object> 列出 <Foo> 当foo是类型变量并且我不知道类型时 - How can I cast List<Object> to List<Foo> when foo is a type variable and I don't know the type 无法从List <Bar>转换为List <Foo> - Cannot convert from List<Bar> to List<Foo> 我该如何修复无法将 Generic.List&lt;&gt; 隐式转换为 Generic.List&lt;&gt; - How can i fix Cannot implicitly convert Generic.List< > to Generic.List< > 我该如何解决无法隐式转换类型System.Collections.Generic.List? - How can i solve Cannot implicitly convert type System.Collections.Generic.List? 如何在列表中转换Uri <string> 列表 <Uri> ? - How can I convert Uri's in List<string> to List<Uri>? 当存在从int到Foo的显式用户定义运算符时,为什么编译器会隐式将double转换为int? - Why does the compiler implicitly convert a double to int when an explicit user-defined operator exists from int to Foo? 如何将类对象转换为列表? - How can I cast a class object to a list? 为什么我不能转换Foo <Bar> 到IFoo <IBar> - Why can I not convert a Foo<Bar> to IFoo<IBar> 如何投放清单 <BaseClass> 列出 <ChildClass> 在C#中? - How can I cast List<BaseClass> to List<ChildClass> in C#? 如何找到具有列表项给定参数的列表项? - how can i find a list item with a list item's given parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM