在 C# 代码中包含数据的格式

发布于 2024-11-08 06:29:31 字数 1186 浏览 4 评论 0原文

我有一个使用硬编码值表的程序。例如,

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独留℉清风醉 2024-11-15 06:29:31

我会(并且很多次)将这种类型的数据推送到文件中(可能通过 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.

挖个坑埋了你 2024-11-15 06:29:31

理想情况下,我想要一个带有自定义 VS2010 查看器的特殊格式文件

这是完全可能的:

  • 创建 DSL
  • 编写支持 DSL 的 VS2010 扩展
  • 编写一个工具来执行构建(MSBuild 任务)
  • 编写一个安装程序来更新 VS 安装工具。

当然,要了解如何做到这一点需要大量学习。

如果您有很多应用程序,并且有很多开发人员使用相同类型的信息,那么这是有意义的。否则还有其他选项:

  1. 配置 Resharper 不重新格式化它(我认为这是可能的)。
  2. 使用 VS 已经可以编辑的数据格式(文本、XML、JSON ...)。在构建时添加为资源,并创建一个工具来在应用程序启动时(或按需)创建对象图。

显然#1(可能的)是最简单的,但很脆弱。 #2 已经很成熟了——这就是资源的处理方式(XAML 也可以如此)。

Ideally I would want a special format file with a custom VS2010 viewer

This is completely possible:

  • Create a DSL
  • Write a VS2010 extension that supports the DSL
  • Write a tool to perform the build (an MSBuild Task)
  • Write an installer to update VS installs with the tooling.

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:

  1. Configure Resharper not to reformat it (I assume this is possible).
  2. Use a data format (text, XML, JSON ...) that VS can already edit. Add as a resource at build time and create a tool to create the object graph at application startup (or on demand).

Clearly #1 (of possible) is simplest, but fragile. #2 is well established -- this is how resources are handled (and XAML can be).

此刻的回忆 2024-11-15 06:29:31

创建设计时 T4 文本模板

void SpecBuild(string spec, string @class, string name, string role)
{
    Write(string.Format("{{ SpecBuild.{0}, " +
                        "new BuildInfo {{ " +
                        "Class = Class.{1}, " +
                        "Name = @\"{2}\", " +
                        "Role = Role.{3} " +
                        "}} " +
                        "}},",
                        spec,
                        @class,
                        name.Replace("\"", "\"\""),
                        role));
}

您可以使用和

//        SpecBuild            Class      Name          Role
//        -------------------- ---------- ------------- -------
SpecBuild("WarriorArms",       "Warrior", "Arms",       "Melee");
SpecBuild("WarriorFury",       "Warrior", "Fury",       "Melee");
SpecBuild("WarriorProtection", "Warrior", "Protection", "Melee");
//        -------------------- ---------- ------------- -------

You could create a design-time T4 text template with

void SpecBuild(string spec, string @class, string name, string role)
{
    Write(string.Format("{{ SpecBuild.{0}, " +
                        "new BuildInfo {{ " +
                        "Class = Class.{1}, " +
                        "Name = @\"{2}\", " +
                        "Role = Role.{3} " +
                        "}} " +
                        "}},",
                        spec,
                        @class,
                        name.Replace("\"", "\"\""),
                        role));
}

and

//        SpecBuild            Class      Name          Role
//        -------------------- ---------- ------------- -------
SpecBuild("WarriorArms",       "Warrior", "Arms",       "Melee");
SpecBuild("WarriorFury",       "Warrior", "Fury",       "Melee");
SpecBuild("WarriorProtection", "Warrior", "Protection", "Melee");
//        -------------------- ---------- ------------- -------
别把无礼当个性 2024-11-15 06:29:31

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文