Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组初始值设定项中的C#源代码格式_C#_Visual Studio_Visual Studio 2010 - Fatal编程技术网

数组初始值设定项中的C#源代码格式

数组初始值设定项中的C#源代码格式,c#,visual-studio,visual-studio-2010,C#,Visual Studio,Visual Studio 2010,在VS.NET 2010中,是否可以在数组初始值设定项中启用C#源代码格式 Elements = { // starting from here source code formatting does not work new TextViewModel("PublicId") { Label = new LText("Fund Nr:", "de", "Fondsnummer"), Watermark = new LText("Please enter fund n

在VS.NET 2010中,是否可以在数组初始值设定项中启用C#源代码格式

Elements =
{
// starting from here source code formatting does not work
  new TextViewModel("PublicId") 
  { 
    Label = new LText("Fund Nr:", "de", "Fondsnummer"),
    Watermark = new LText("Please enter fund number...", "de", "Bitte hier Fondsnummer erfassen...")
  },
  new CheckViewModel("Juristic")
  {
    Style = "Radio",
    Default = true,
    Label = new LText("Juristic Fund", "de", "Juristischer Fonds")
  }
}    
// starting from here source code formatting works again

我是否缺少工具->选项->文本编辑器->C#?

中的一些格式选项?我相信您的TextViewModel和CheckViewModel上需要一个默认构造函数

据我所知,这是.net能够做到这一点的唯一方法。另外,我假设这两个对象继承自基类,并且数组的类型与基类的类型相同

否则,创建的对象将是“var”类型。编译器抛出了一个fit,因为从技术上讲,数组也必须是同一类型的“var”


此外,传递给对象的CTOR的参数应该在属性中初始化,就像TextViewModel对象的属性标签和水印,以及CheckViewModel的样式、默认值和标签一样。

这看起来不像有效的C。在
元素=
之后缺少数组声明。应该是这样的:

Elements = new ViewModelBase[] { /* ... */ };

一旦您解决了这个问题,格式可能会开始正常工作。对我来说是这样的(但我正在使用ReSharper)。

看起来这是VS.NET的“功能”

这是一个“无法修复的功能”

但是,正如微软在那里发布的:

如果其他人发现这是一项重要功能,请继续投票 把它弄起来


注意这仅适用于多行初始值设定项。

元素是与数组兼容的属性(列表)。代码段取自对象初始值设定项。获取。在这种情况下,答案可能是:“不,您没有遗漏任何选项;Visual Studio无法正确格式化。请在Connect上提交一个bug(使用更多上下文)或安装ReSharper,这样就可以了。”我的ReSharper历史记录不好。尝试了几次,但在它被卸载后,VS.NET就像呼吸了一口新鲜空气——快速而愤怒:)这与构造器无关。这只是VisualStudio的一个bug或功能。VS.NET根本没有智能格式化数组初始值设定项:(根据您对我的回答的评论:“代码片段取自对象初始值设定项。”--您有没有可能添加更多上下文?我已经用
var foo=new foo{Elements=…}试过了
,它是有效的C#。它与您的代码片段中的不一样。到vs2013为止,数组和列表初始值设定项的问题仍然存在