我们可以在使用 XmlSerializer 时将 Flag 枚举值标记为过时吗?
Can we mark a Flag enumeration value as obsolete when using XmlSerializer?
我看到了这个 [问题][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">
相当简单的东西。现在,我在 class:
中添加了一个升级方法
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 文件。然后它在文件上运行升级方法以使其更新。
此时,当 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
关键字毕竟是要走的路。
我看到了这个 [问题][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">
相当简单的东西。现在,我在 class:
中添加了一个升级方法 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 文件。然后它在文件上运行升级方法以使其更新。
此时,当 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
关键字毕竟是要走的路。