c# - 跨多个属性的 XMLSerialize CategoryAttribute

标签 c# visual-studio xml-serialization

C#.net 2008

我正在处理一个包含大约 8 个序列化类别的数百个属性的类文件。 目前它看起来像这样:

[CategoryAttribute("Group1")]
public string property
[CategoryAttribute("Group1")]
public string property
[CategoryAttribute("Group1")]
public string property
[CategoryAttribute("Group1")]
public string property

[CategoryAttribute("Group2")]
public string property
[CategoryAttribute("Group2")]
public string property
[CategoryAttribute("Group2")]
public string property
[CategoryAttribute("Group2")]
public string property

我过去做过研究,但没有找到任何解决方案的线索,所以这次我想问一下,是否有人有解决方案来让事情变得不那么困惑? 如:

[CategoryAttribute("Group1")]
{
public string property;
public string property;
public string property;
public string property;
public string property;
public string property;
public string property;
}
[CategoryAttribute("Group2")]
{
public string property;
public string property;
public string property;
public string property;
public string property;
public string property;
public string property;
}

最佳答案

虽然所有其他答案都是完全正确的,但我可以向您提出另一个想法。

您可以尝试使用 T4 模板生成此类文件。 请在 GitHub 查看我的示例

基本上我创建了一个模板/EntitiesWithALotOfProperties/FirstClass.generated.tt,它读取一个同名的文件,但将 ".generated.tt" 替换为 < em>".template.cs" 并根据您的逻辑处理后者。

需要注意的重要事项:FirstClass.template.cs 应该将构建操作设置为“None”。想法是:*.cs 文件可以使用 ReSharper 轻松编辑(基本上具有 IntelliSense),但与此同时,它不会声明重复的成员。

以下是部分截图: Another partial part Template file Generated file

关于c# - 跨多个属性的 XMLSerialize CategoryAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8176876/

相关文章:

c# - 如何从对象列表创建 xml 文件

c# - XMLSerializer 没有正确地为 List 类型的属性反序列化 XML

c# - 检测文本框中的小数

c# - 如何在 Java 中使用 .Net 程序集

c# - 使用 gsm 调制解调器从 C# 应用程序发送接收短信的基本概念

c++ - VS2010、DirectX 和 Intellisense 问题

c++ - 什么是 Visual Studio 项目引用?

.net - 不编码插件时获取当前的 EnvDTE 或 IServiceProvider

c# - 从 XML 数据反序列化数组(在 ServiceStack 中)

c# - AutoMapper 的映射问题?