如何使用 R# 格式化静态变量声明?

发布于 2024-11-10 10:09:25 字数 816 浏览 4 评论 0原文

    private static readonly Dictionary<int, LocalizationLanguage> _languages = new Dictionary<int, LocalizationLanguage>() {
    {0,new LocalizationLanguage { CultureInfo = "en-US", Id = 0 }},
{1,new LocalizationLanguage { CultureInfo = "es-AR", Id = 1 }}
    };

我有这个声明,它不会自动包装成好的东西,而如果我做完全相同的事情,但使用属性访问器,它的格式完美地为:

    private static Dictionary<int, LocalizationLanguage> _languages
    {
        get
        {
            return new Dictionary<int, LocalizationLanguage>()
               {
                   {0, new LocalizationLanguage {CultureInfo = "en-US", Id = 0}},
                   {1, new LocalizationLanguage {CultureInfo = "es-AR", Id = 1}}
               };
        }
    }

这是什么原因以及如何使 vs2010 或 R# 自动 -格式化这种表达式?

    private static readonly Dictionary<int, LocalizationLanguage> _languages = new Dictionary<int, LocalizationLanguage>() {
    {0,new LocalizationLanguage { CultureInfo = "en-US", Id = 0 }},
{1,new LocalizationLanguage { CultureInfo = "es-AR", Id = 1 }}
    };

I have this declaration, and it doesn't autowrap into something nice, whereas if I do the exact same but with a property accessor, it formats perfectly to:

    private static Dictionary<int, LocalizationLanguage> _languages
    {
        get
        {
            return new Dictionary<int, LocalizationLanguage>()
               {
                   {0, new LocalizationLanguage {CultureInfo = "en-US", Id = 0}},
                   {1, new LocalizationLanguage {CultureInfo = "es-AR", Id = 1}}
               };
        }
    }

What's the reason for this and how can I make either vs2010 or R# auto-format this kind of expressions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

甜点 2024-11-17 10:09:25

我刚刚自己测试过,因为我自己使用 vs2010 和 R#。它会自动将代码重新格式化为:

private static readonly Dictionary<int, LocalizationLanguage> _languages =
    new Dictionary<int, LocalizationLanguage>()
        {
            { 0, new LocalizationLanguage { CultureInfo = "en-US", Id = 0 } },
            { 1, new LocalizationLanguage { CultureInfo = "es-AR", Id = 1 } }
        };

vs2010 在完成语句(输入 ;)后重新格式化代码。我通过将未格式化的代码粘贴到 Visual Studio 中进行测试,并删除最后的 };。再次输入会自动格式化。我还在关闭语句之前测试了原始代码的不同变体,并且无论如何它的格式都是相同的。

我猜你的 vs2010 或 R# 在这种情况下有问题?

I've just tested this myself, as I use vs2010 and R# myself. It automatically reformats the code to:

private static readonly Dictionary<int, LocalizationLanguage> _languages =
    new Dictionary<int, LocalizationLanguage>()
        {
            { 0, new LocalizationLanguage { CultureInfo = "en-US", Id = 0 } },
            { 1, new LocalizationLanguage { CultureInfo = "es-AR", Id = 1 } }
        };

vs2010 reformats the code upon completion of the statement (entering the ;). I tested by pasting your unformatted code into visual studio, and erasing the final };. Typing it again automatically formats it. I also tested having different variations of the original code before closing the statement and it formats it the same no matter what.

I would guess there is an issue with your vs2010 or R# in this case?

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