2013-10-03 11 views
5

Sử dụng protobuf-net v2 build 668, tôi đang cố gắng tuần tự hóa/deserialize một lớp có chứa thành viên được định nghĩa là giao diện trong khi thực hiện chuyển đổi on-the-fly cùng một lúc. Thông thường, cách tiếp cận thay thế sẽ hoạt động tốt nhưng vì C# không cho phép chuyển đổi do người dùng xác định cho giao diện, tôi không thể xác định chuyển đổi.Có cách nào để xác định các hàm chuyển đổi thay thế (từ/sang giao diện) trong lớp thay thế protobuf-net

Cảm ơn,

namespace ProtoBufNetTest 
{ 
    using System.Diagnostics; 
    using System.IO; 

    using ProtoBuf; 
    using ProtoBuf.Meta; 

    class Program 
    { 
     static void Main() 
     { 
      RuntimeTypeModel.Default.Add(typeof(IDummy), false) 
       .SetSurrogate(typeof(DummySurrogate)); 

      var container = new Container { Data = new Dummy { Positive = 3 } }; 

      using (var file = File.Create("test.bin")) 
      { 
       Serializer.Serialize(file, container); 
      } 

      using (var file = File.OpenRead("test.bin")) 
      { 
       container = Serializer.Deserialize<Container>(file); 
       Debug.Assert(container.Data.Positive == 3); 
      } 
     } 
    } 

    // Outside of the project, cannot be changed 
    public interface IDummy 
    { 
     int Positive { get; set; } 
    } 

    [ProtoContract] 
    public class Container 
    { 
     [ProtoMember(1)] 
     public IDummy Data { get; set; } 
    } 

    public class Dummy : IDummy 
    { 
     public int Positive { get; set; } 
    } 

    [ProtoContract] 
    class DummySurrogate 
    { 
     [ProtoMember(1)] 
     public int Negative { get; set; } 

     // Does not compile : user-defined conversions to or from an interface are not allowed 
     public static explicit operator IDummy(DummySurrogate value) 
     { 
      return value == null ? null : new Dummy { Positive = -value.Negative }; 
     } 

     // Does not compile : user-defined conversions to or from an interface are not allowed 
     public static explicit operator DummySurrogate(IDummy value) 
     { 
      return value == null ? null : new DummySurrogate { Negative = -value.Positive }; 
     } 

     // Fake attribute, does not exist but could work if it did 
     [ProtoConvertFrom] 
     public static IDummy From(DummySurrogate value) 
     { 
      return value == null ? null : new Dummy { Positive = -value.Negative }; 
     } 

     // Fake attribute, does not exist but could work if it did 
     [ProtoConvertTo] 
     public static DummySurrogate To(IDummy value) 
     { 
      return value == null ? null : new DummySurrogate { Negative = -value.Positive }; 
     } 
    } 
} 
+0

Chỉ cần nói: Tôi không bỏ qua bạn - do di chuyển nhà tôi đã giới hạn thời gian sử dụng máy tính hiện nay. Sẽ lấy lại cho bạn càng sớm càng tốt –

+0

Cảm ơn Marc. Chúc may mắn với sự di chuyển. –

Trả lời

3

Trong xây dựng hiện nay: không có, không có.

Tuy nhiên, trong việc xây dựng tiếp theo, điều này hoạt động tốt:

[ProtoContract] 
class DummySurrogate 
{ 
    [ProtoMember(1)] 
    public int Negative { get; set; } 

    [ProtoConverter] 
    public static IDummy From(DummySurrogate value) 
    { 
     return value == null ? null : new Dummy { Positive = -value.Negative }; 
    } 

    [ProtoConverter] 
    public static DummySurrogate To(IDummy value) 
    { 
     return value == null ? null : new DummySurrogate 
      { Negative = -value.Positive }; 
    } 
} 

Về cơ bản, một phương pháp static đánh dấu [ProtoConverter] được ưu tiên hơn một nhà điều hành implicit hoặc explicit chuyển đổi được xác định, với lợi thế thêm rằng [ProtoConverter] phương pháp này không tuân theo các quy tắc cú pháp giống như các toán tử. Không cần phải xác định các thuộc tính riêng biệt *To/*From vì mục đích rõ ràng là từ chữ ký.

Lưu ý: các thuộc tính trên Dummy là không cần thiết và không bao giờ được sử dụng.

+0

Thật hoàn hảo. Nhìn về phía trước để xây dựng tiếp theo. Cảm ơn rất nhiều. Franck –

+0

Tôi đã xóa các thuộc tính trên Dummy để tránh bất kỳ sự nhầm lẫn nào. –

+0

Marc, có vẻ như bản xây dựng này chưa bao giờ được phát hành cho NuGet, có kế hoạch nào cho nó hay tôi nên xây dựng protobuf-net từ nguồn không? –

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