使用XmlSerializer时,我们可以将Flag枚举值标记为过时吗?

问题描述

我已经看到这个[question] [1],它指向[Obsolete("...")],听起来像是我可能需要使用的东西。但是现在我认为我做不到。

我有以下更新的标志枚举:

[Flags]
[Guid("xxxxx")]
[ComVisible(true)]
public enum AssignmentType
{
    None = 0,Attendant = 1,ConductorCBS = 2,ReaderCBS = 4,Chairman = 8,Mike = 16,PlatformAttendant = 32,Prayer = 64,OCLM = 128,Sound = 256,Student = 512,Custom = 1024,Demonstration = 2048,Assistant = 4096,Host = 8192,CoHost = 16384,OCLMTreasures = 32768,OCLMGems = 65536,OCLMLiving = 131072
}

为了给您一些背景信息,这些标志枚举如下所示出现在我的XML文件中:

      <Assignments Attendant="false" ConductorCBS="false" ReaderCBS="false" Chairman="false" Microphones="false" PlatformAttendant="false" Prayer="false" OCLM="false" Sound="false" Student="false" Assistant="false" Host="false" CoHost="false" OCLMTreasures="false" OCLMGems="true" OCLMLiving="false" Demonstrations="false">

相当简单的东西。现在,我向班级添加升级方法

    public bool UpgradePublisherDatabase()
    {
        bool bSaveDB = false;

        try
        {
            // Database should already be open

            if(PublisherData.Version < 1)
            {
                // CODE SNIPPED

                bSaveDB = true;
            }
            
            if(PublisherData.Version < 2)
            {
                // We need to upgrade the database
                PublisherData.Version = 2;

                var vPublisherKeys = new List<string>(PublisherData.PublisherDictionary.Keys);
                foreach (string strPublisherKey in vPublisherKeys)
                {
                    Publisher oPublisher = PublisherData.PublisherDictionary[strPublisherKey];
                    if (oPublisher.CanUseFor(AssignmentType.OCLM))
                    {
                        // We must set the other OCLM flags
                        oPublisher.SetCanUseFor(true,AssignmentType.OCLMTreasures);
                        oPublisher.SetCanUseFor(true,AssignmentType.OCLMGems);
                        oPublisher.SetCanUseFor(true,AssignmentType.OCLMLiving);
                    }

                    PublisherData.PublisherDictionary[strPublisherKey] = oPublisher;
                }

                bSaveDB = true;
            }

            if(bSaveDB)
            {
                return SavePublisherData();
            }
        }
        catch (Exception ex)
        {
            SimpleLog.Log(ex);
            return false;
        }

        return true;
    }

在我的主应用程序(Visual C ++ MFC)中,它调用我的DLL来加载XML文件。然后,它在文件上运行Upgrade方法以使其保持最新状态。

这一次,当XML文件为版本2时,由于不再使用XML文件,因此不再需要AssignmentType.OCLM标志。但是似乎不可能在序列化时仅将标记枚举值标记为过时吗?


更新

这是该属性的定义:

   [XmlAttribute("OCLM")]
    public bool UseForOCLMAssignments
    {
        get => _Assignments.HasFlag(AssignmentType.OCLM); set
        {
            if (value)
                _Assignments |= AssignmentType.OCLM;
            else
                _Assignments &= ~AssignmentType.OCLM;
        }
    }

所以,我希望能够读入它,但我不想写出来。那么在这里使用[Obsolete...]语法是否可以接受? [1]:Obsolete attribute causes property to be ignored by XmlSerialization

解决方法

最后,这就是我所需要的:


    [Obsolete("Use the OCLMTreasures,OCLMGems and OCLMLiving flag values instead.")]
    [XmlAttribute("OCLM")]
    public bool UseForOCLMAssignments
    {
        get => _Assignments.HasFlag(AssignmentType.OCLM); set
        {
            if (value)
                _Assignments |= AssignmentType.OCLM;
            else
                _Assignments &= ~AssignmentType.OCLM;
        }
    }

毕竟,Obsolete关键字是解决之道。