在 C# 代码中包含数据的格式
我有一个使用硬编码值表的程序。例如,
public static readonly IDictionary<SpecBuild, BuildInfo> Builds = new Dictionary<SpecBuild, BuildInfo> {
{ SpecBuild.WarriorArms,
new BuildInfo {
Class = Class.Warrior,
Name = "Arms",
Role = Role.Melee
}
},
{ SpecBuild.WarriorFury,
new BuildInfo {
Class = Class.Warrior,
Name = "Fury",
Role = Role.Melee
}
},
{ SpecBuild.WarriorProtection,
new BuildInfo {
Class = Class.Warrior,
Name = "Protection",
Role = Role.Tank
}
}, ...
此数据很少更改,因此我可以对其进行硬编码。问题是很难读取这些数据,并且格式化不起作用,因为 Resharper 会以不同的方式重新格式化它。
理想情况下,我需要一个带有自定义 VS2010 查看器的特殊格式文件,该文件会将此信息显示为表格,并在编译过程中为这些表格生成 C# 代码,或者甚至可能将其直接编译到 MSIL (CLR) 中。
我正在寻找交钥匙解决方案,问题还没有严重到需要任何额外的开发。
I have a program that uses tables of hard-coded values. E.g.
public static readonly IDictionary<SpecBuild, BuildInfo> Builds = new Dictionary<SpecBuild, BuildInfo> {
{ SpecBuild.WarriorArms,
new BuildInfo {
Class = Class.Warrior,
Name = "Arms",
Role = Role.Melee
}
},
{ SpecBuild.WarriorFury,
new BuildInfo {
Class = Class.Warrior,
Name = "Fury",
Role = Role.Melee
}
},
{ SpecBuild.WarriorProtection,
new BuildInfo {
Class = Class.Warrior,
Name = "Protection",
Role = Role.Tank
}
}, ...
This data seldom changes, so I am fine with hard-coding it. The problem is that it is hard to read this data, and formatting doesn't work because Resharper reformats it differently.
Ideally I would want a special format file with a custom VS2010 viewer that would present this info as a table and either generate a C# code for those table as part of compilation process, or maybe even compile it directly into MSIL (CLR).
I am looking for a turnkey solution, the problem is not severe enough to warrant any extra development.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会(并且很多次)将这种类型的数据推送到文件中(可能通过 resx)。然后通过您选择的解串器加载数据。如果需要轻松编辑,可以通过 JavaScriptSerializer 或 JSON.NET 使用 JSON。如果被告知数据和性能是关键,也许是更硬核的东西(也许是二进制的)。 XML 当然也是一个可行的选择。
如果场景允许,其他选项可能包括嵌入式数据库。
I would (and have, many times) push this type of data into a file (perhaps via a resx). Then load the data via your choice of deserializer. If it needs to be easily edited, maybe JSON via JavaScriptSerializer or JSON.NET. If it is told of data and performance is key, maybe something more hardcore (maybe binary). XML is of course a viable choice too.
Other options might included an embedded database, if the scenario warrants it.
这是完全可能的:
当然,要了解如何做到这一点需要大量学习。
如果您有很多应用程序,并且有很多开发人员使用相同类型的信息,那么这是有意义的。否则还有其他选项:
显然#1(可能的)是最简单的,但很脆弱。 #2 已经很成熟了——这就是资源的处理方式(XAML 也可以如此)。
This is completely possible:
Of course that is going to be a lot of learning to understand how to do it.
If you have lots of applications with lots of developers using the same type of information it would make sense. Otherwise there are other options:
Clearly #1 (of possible) is simplest, but fragile. #2 is well established -- this is how resources are handled (and XAML can be).
创建设计时 T4 文本模板
您可以使用和
You could create a design-time T4 text template with
and
Resharper 允许我按扩展名排除文件,因此我将文件命名为 *.data.cs,根据需要格式化数据,并且 resharper 不会重新格式化它。
Resharper lets me exclude files by extension, so I name my files *.data.cs, format data as I want it, and resharper won't reformat it.