2009-12-28 69 views
7

Tôi đang tạo toán tử chuyển đổi rõ ràng để chuyển đổi giữa danh sách chung loại thực thể thành danh sách chung của các loại mô hình. Có ai biết tại sao tôi nhận được lỗi sau:lỗi toán tử chuyển đổi rõ ràng khi chuyển đổi danh sách chung

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

Tôi đã có toán tử chuyển đổi rõ ràng giữa Entity.objA và Model.objA hoạt động tốt. Vấn đề phát sinh khi cố gắng chuyển đổi danh sách chung chung. Điều này thậm chí có thể?

Đây là mã của tôi:

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; 
    } 

Cảm ơn sự giúp đỡ nào.

Trả lời

17

Lỗi "Chuyển đổi do người dùng xác định phải chuyển đổi thành hoặc từ loại bao quanh" nói chính xác ý nghĩa của nó. Nếu bạn có nhà điều hành chuyển đổi

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

Sau đó, xxx phải là MyClass. Đây là ý nghĩa của "chuyển đổi phải chuyển đổi thành hoặc từ loại bao quanh." Kiểu kèm theo ở đây là MyClass.

Phần liên quan của ECMA334 C# spec là 17.9.4:

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 and T0 are different types.

Either S0 or T0 is the class or struct type in which the operator declaration takes place.

Neither S0 nor T0 is an interface-type.

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

Vì vậy, đây là mã của bạn:

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; 
} 

Vấn đề là cho điều này phải được xác định như một nhà điều hành chuyển đổi nó phải nằm trong các lớp học List<Model.objA> hoặc List<Entity.objA> nhưng tất nhiên bạn không thể làm điều đó vì bạn không có quyền truy cập để thay đổi các loại đó.

Bạn có thể sử dụng Enumerable.Select để chiếu loại khác hoặc List<T>.ConvertAll. Ví dụ:

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

Về cơ bản, bạn không thể thực hiện việc này. Trong toán tử của bạn, kiểu đầu vào hoặc đầu ra phải thuộc loại khai báo toán tử. Một tùy chọn có thể là một phương pháp mở rộng, nhưng thành thật mà nói LINQ Select khá gần với chính nó, cũng như List<T>.ConvertAll.

Như một ví dụ về cách tiếp cận phương pháp khuyến nông:

public static class MyListUtils { 
    public static List<Model.objA> ToModel(this List<Entity.objA> entities) 
    { 
    return entities.ConvertAll(entity => (Model.objA)entity); 
    } 
} 

hoặc tổng quát hơn với LINQ Select:

public static class MyListUtils { 
    public static IEnumerable<Model.objA> ToModel(
    this IEnumerable<Entity.objA> entities) 
    { 
    return entities.Select(entity => (Model.objA)entity); 
    } 
} 

và chỉ sử dụng someList.ToModel().

Các vấn đề liên quan