通用转换列表时显式转换操作失误 [英] explicit conversion operator error when converting generic lists

查看:177
本文介绍了通用转换列表时显式转换操作失误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个显式转换运营商的实体类型的泛型列表之间进行转换,以模型类型的泛型列表。有谁知道为什么我收到以下错误:

I am creating an explicit conversion operator to convert between a generic list of entity types to a generic list of model types. Does anyone know why I get the following error:

用户定义的转换必须转换或者从封闭类型

User-defined conversion must convert to or from the enclosing type

我已经有Entity.objA和Model.objA之间的显式转换运算符,它工作正常。尝试转换的泛型列表时,问题出现了。这甚至可能

I already have an explicit conversion operator between Entity.objA and Model.objA which works fine. The problem arises when trying to convert the generic list. Is this even possible?

下面是我的代码:

    public static explicit operator List<Model.objA>(List<Entity.objA> entities)
    {
        List<Model.objA> objs= new List<Model.objA>();
        foreach (Entity.objA entity in entities)
        {
            objs.Add((Model.objA)entity);
        }
        return claims;
    }



感谢您的帮助。

Thanks for any help.

推荐答案

错误用户定义的转换必须转换或者从封闭类型确切说是什么意思。如果你有一个转换操作符

The error "User-defined conversion must convert to or from the enclosing type" says exactly what it means. If you have a conversion operator

class MyClass {
    public static explicit operator xxx(string s) { // details }
    public static implicit operator string(xxx x) { // details }
}

然后 XXX 必须 MyClass的。这是由指转换必须转换或从封闭类型。这里的封闭类型为 MyClass的

Then xxx must be MyClass. This is what is meant by the "conversion must convert to or from the enclosing type." The enclosing type here is MyClass.

ECMA334 C#规范的相关部分17.9.4:

The relevant section of the ECMA334 C# spec is 17.9.4:

一个转换操作符从源类型,由参数的类型转换操作符的指示,到目标类型,通过的返回类型指示转换转换操作符。类或结构允许声明从源类型S转换到目标类型T仅在以下所有条件都为真,其中S0和T0是从删除尾部产生的类型?改性剂,如果有的话,选自S和T:

A conversion operator converts from a source type, indicated by the parameter type of the conversion operator, to a target type, indicated by the return type of the conversion operator. A class or struct is permitted to declare a conversion from a source type S to a target type T only if all of the following are true, where S0 and T0 are the types that result from removing the trailing ? modifiers, if any, from S and T:

S0和T0是不同类型的

S0 and T0 are different types.

<强>无论是S0或T0是在运算符声明发生在类或结构类型。

无论是S0也不T0是一个接口类型。

Neither S0 nor T0 is an interface-type.

不包括用户定义的转换,转换不选自S存在T或从T到秒。

Excluding user-defined conversions, a conversion does not exist from S to T or from T to S.

因此,这里是你的代码:

So here's your code:

public static explicit operator List<Model.objA>(List<Entity.objA> entities) {
    List<Model.objA> objs= new List<Model.objA>();
    foreach (Entity.objA entity in entities) {
        objs.Add((Model.objA)entity);
    }
    return claims;
}



的问题是,对于此被定义为一个转换算它必须驻留在列表< Model.objA> 列表< Entity.objA> 类,但你当然可以这样做因为你没有权限更改这些类型。

The issue is that for this to be defined as a conversion operator it must reside in the List<Model.objA> or List<Entity.objA> classes but of course you can not do that as you don't have access to change those types.

您可以使用 Enumerable.Select 来投射到其他类型的,或列表< T> .ConvertAll 。例如:

You could use Enumerable.Select to project to the other type, or List<T>.ConvertAll. For example:

public static class ListExtensions {
    public static List<Model.objA> ConvertToModel(this List<Entity.objA> entities) {
        return entities.ConvertAll(e => (Model.objA)e);
    }
}

这篇关于通用转换列表时显式转换操作失误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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