代码之家  ›  专栏  ›  技术社区  ›  Chris Doggett

添加自动属性会影响远程处理吗?

  •  2
  • Chris Doggett  · 技术社区  · 17 年前

    1. 早在我来到这里之前,所有DAO都被定义为使用字段而不是属性,并且不能将字段绑定到控件。

    使用一个简单的、人为的和想象的例子,可以改变对象:

    public class Employee
    {
        public int ID;
        public string Name;
        public DateTime DateOfBirth;
    }
    

    为此:

    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime DateOfBirth { get; set; }
    }
    

    更改序列化格式,破坏与旧客户端的兼容性?

    1 回复  |  直到 17 年前
        1
  •  1
  •   Marc Gravell    17 年前

    重要编辑:这应该兼容并允许绑定?

    public class Employee
    {
        private int id;
        private string name;
        private DateTime dateOfBirth;
        public int ID { get {return id;} set {id = value;} }
        public string Name { get {return name;} set {name = value;} }
        public DateTime DateOfBirth { get {return dateOfBirth;}
             set {dateOfBirth = value;} }
    }
    

    当然值得一试,不是吗?

    是的,如果客户端/服务器不同步,这将导致问题。

    .NET远程处理使用BinaryFormatterm,它(没有定制的 ISerializable 实现)使用字段名。使用“自动属性”会断开字段名。

    protobuf-net . 如果您愿意,我可以提供一个示例(它支持 ISerializable 用法)。

    (顺便说一句,添加属性应该 影响 BinaryFormatter ,因为它是基于字段的)


    控制远程处理序列化(直接从我的 unit tests ); 请注意,这将 而且 二进制格式 )等(介于两者之间的一切)。。。这只是

    [Serializable, ProtoContract]
    public sealed class ProtoFragment : ISerializable
    {
        [ProtoMember(1, DataFormat=DataFormat.TwosComplement)]
        public int Foo { get; set; }
        [ProtoMember(2)]
        public float Bar { get; set; }
    
        public ProtoFragment() { }
        private ProtoFragment(
            SerializationInfo info, StreamingContext context)
        {
            Serializer.Merge(info, this);
        }
        void  ISerializable.GetObjectData(
            SerializationInfo info, StreamingContext context)
        {
            Serializer.Serialize(info, this);
        }
    }
    

    在这里,下面的两种方法满足 ,只需将执行传递给 protobuf网 发动机这个 [ProtoMember(...)] 定义字段(具有唯一标识标记)。如前所述,它也可以推断这些,但更安全(不太脆弱)的是明确。

    推荐文章