集合上的隐式转换 [英] Implicit Conversion over a Collection

查看:81
本文介绍了集合上的隐式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本周,我遇到了有关集合中C#隐式转换的问题.尽管这(使用implicit)可能不是我们最终的方法,但我至少希望完成代码以提供团队选择.我将问题归结为以下示例:

I ran into a problem this week regarding implicit conversions in C# on collections. While this (using implicit) may not be our final approach, I wanted to at least finish out the code to offer the team as an option. I boiled down the problem to the following sample case:

我的示例中有两个类:一个代表一个业务对象(Foo),另一个代表该业务项目(FooVO)的客户端版本(查看对象),如下所示...

I have two classes in my example: one that represents a business object (Foo) and one that represents the client version (View Object) of this business item (FooVO), as defined below...

public class Foo
{
    public string Id {get; set;}

    public string BusinessInfo {get; set;}
}

public class FooVO
{
    public string Id {get; set;}

    public static implicit operator FooVO( Foo foo )
    {
        return new FooVO { Id = foo.Id };
    }
}

我的问题是,当我有一个Foo对象列表并想使用隐式运算符将它们转换为FooVO对象列表时.

My problem is when I have a a List of Foo objects and want to convert them to a list of FooVO objects using my implicit operator.

List<Foo> foos = GetListOfBusinessFoos(); // Get business objects to convert

我尝试过

List<FooVO> fooVOs = foos; // ERROR

List<FooVO> fooVOs = (List<FooVO>) foos; // ERROR

甚至

List<FooVO> fooVOs = foos.Select( x => x ); // ERROR

我知道我可以循环执行此操作,但是我希望采用一种简单的(LINQ?)方法将对象转换为一张照片.有什么想法吗?

I know I can do this in a loop, but I was hoping for straightforward (LINQ?) way to convert the objects in one shot. Any ideas?

谢谢.

编辑 修复示例中的错字

推荐答案

几乎每天都这样问一个类似这样的问题.您无法执行此操作,因为这样做违反了类型安全性:

A question much like this gets asked almost every day on SO. You can't do this because doing so violates type safety:

List<Giraffe> g = new List<Giraffe>();
List<Animal> a = g; // Should this be legal?
a.Add(new Tiger()); // Nope; we just added a tiger to a list of giraffes.

在C#4.0中,您可以从IEnumerable<Giraffe>隐式转换为IEnumerable<Animal>,因为没有添加"方法可以解决问题.但是,如果元素类型的转换是用户定义的,则您永远无法像这样进行协变"转换.它必须是引用身份转换.

In C# 4.0 you can implicitly convert from IEnumerable<Giraffe> to IEnumerable<Animal> because there is no "Add" method to screw things up. But you can never do a "covariant" conversion like that if the conversion of the element types is user-defined. It has to be a reference or identity conversion.

您需要创建第二个列表,然后一次复制一个.或使用Select和ToList之类的LINQ帮助器方法为您完成这项工作.

You'll need to create a second list and copy them over one at a time. Or use the LINQ helper methods like Select and ToList to do that work for you.

所需的类型系统概念的名称为协方差";协变量关系是您认为长颈鹿是动物,因此长颈鹿序列是动物序列"的关系.如果您对此主题感兴趣,那么您可以在此处了解我们如何将协方差(和逆方差)添加到C#4.0中:

The name of the type system concept you want is "covariance"; a covariant relationship is one where you reason "Giraffes are animals therefore sequences of giraffes are sequences of animals". If this subject interests you then you can read about how we added covariance (and contravariance) to C# 4.0 here:

http://blogs.msdn. com/b/ericlippert/archive/tags/covariance + and + contravariance/default.aspx

从底部开始.

这篇关于集合上的隐式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆