"InvalidDataContractException: Type contains two members with same data member name" 无缘无故

"InvalidDataContractException: Type contains two members with same data member name" for no apparent reason

我有以下类型:

基本类型:

   [DataContract(Name = "protectedFile", Namespace = Constants.ManagementNamespace)]
    public class ProtectedFile
    {
        public ProtectedFile(string virtualDiskName, FileIdInfo fileIdInfo)
        {
            VirtualDiskName = virtualDiskName;
            FileIdInfo = fileIdInfo;
        }

    /// <summary>
    /// Gets or sets file id information
    /// </summary>
    [DataMember(Name = "fileIdInfo")]
    public FileIdInfo FileIdInfo { get; set; }

    /// <summary>
    /// Gets or sets Virtual Disk Name
    /// </summary>
    [DataMember(Name = "virtualDiskName")]
    public string VirtualDiskName { get; set; }

      // .. Equality members

    }

ProtectedFile 包含类型:

    [Serializable]
    [DataContract(Name = "FileIdInfo", Namespace = Constants.ManagementNamespace)]
    public sealed class FileIdInfo
    {
        public FileIdInfo()
        {
        }

        public FileIdInfo(FileSystemFileIdInformation fsFileId, int driverFileId)
        {
            DriverFileId = driverFileId;
            FsFileId = fsFileId;
        }

        /// <summary>
        /// Gets or sets file system file id information
        /// </summary>
        [DataMember(Name = "fsFileId")]
        public FileSystemFileIdInformation FsFileId { get; set; }

        /// <summary>
        /// Gets or sets driver file id information
        /// </summary>
        [DataMember(Name = "driverFileId")]
        public int DriverFileId { get; set; }

        // .. Equality members
    }


    [Serializable]
    [DataContract(Name = "FileSystemFileIdInformation", Namespace = Constants.ManagementNamespace)]
    [StructLayout(LayoutKind.Sequential)]
    public struct FileSystemFileIdInformation
    {
        [DataMember(Name = "VolumeSerialNumber")]
        private readonly ulong _volumeSerialNumber;

        [DataMember(Name = "FileId")]
        private readonly FileId128 _fileId;

        public ulong VolumeSerialNumber
        {
            get { return _volumeSerialNumber; }
        }

        public FileId128 FileId
        {
            get { return _fileId; }
        }

        // .. Equality members
    }

    /// <summary>
    /// File Identifier inside the volume (FILE_ID_128 at ntifs.h/winnt.h)
    /// </summary>
    [Serializable]
    [DataContract(Name = "FileId128", Namespace = Constants.ManagementNamespace)]
    [StructLayout(LayoutKind.Sequential)]
    public struct FileId128
    {
        [DataMember(Name = "highPart")]
        private readonly ulong _highPart;

        [DataMember(Name = "lowPart")]
        private readonly ulong _lowPart;

        public ulong HighPart
        {
            get { return _highPart; }
        }

        public ulong LowPart
        {
            get { return _lowPart; }
        }

        // .. Equality members
    }

    /// <summary>
    /// Internal file system file id - AAFILE_ID_INFORMATION from AAFileFilterCommon.h
    /// </summary>
    [Serializable]
    [DataContract(Name = "FileSystemFileIdInformation", Namespace = Constants.ManagementNamespace)]
    [StructLayout(LayoutKind.Sequential)]
    public struct FileSystemFileIdInformation
    {
        [DataMember(Name = "VolumeSerialNumber")]
        private readonly ulong _volumeSerialNumber;

        [DataMember(Name = "FileId")]
        private readonly FileId128 _fileId;

        public ulong VolumeSerialNumber
        {
            get { return _volumeSerialNumber; }
        }

        public FileId128 FileId
        {
            get { return _fileId; }
        }

        // .. Equality members
    }

一切看起来都很好,每个数据契约成员都有一个唯一的名称,但我得到一个序列化异常

无效数据合同异常:
FileIdInfo 类型包含两个相同的成员 FsFileId 和 DriverFileId
数据成员名称 fsFileId

但是我只有一个成员叫这个名字。唯一不寻常的是我使用结构作为数据成员,但这是受支持的。

怎么了?

我无法重现这个,以防万一 - 你在

复制了 2 次 FileSystemFileIdInformation 结构

ProtectedFile contains types:

我写了一个简单的序列化示例,它在两种方式(序列化和反序列化)中都没有任何异常——我唯一的变化是我为只读参数添加了构造函数,但这对序列化来说真的无关紧要。

var protectedFile=
new ProtectedFile("C",new FileIdInfo(new FileSystemFileIdInformation(
                        1, new FileId128(2,3)),
                4));
            DataContractSerializer d = new DataContractSerializer(protectedFile.GetType());
            var stream = new MemoryStream();
            d.WriteObject(stream, protectedFile);
            stream.Seek(0, SeekOrigin.Begin);
            var fileAfterSerialization = d.ReadObject(stream);