使用大括号而不定义函数,结构等

时间:2013-07-26 12:09:38

标签: c# orchardcms braces

在分析Orchard.cms源代码时,我发现了有趣的代码,当C#中的括号不用于定义任何内部对象时。  似乎它是为某些上下文使用而制作的。你能否澄清一下使用这样的括号的目标是什么?

以下是代码示例:

 builder.RegisterType<DefaultOrchardHost>().As<IOrchardHost>().As<IEventHandler>().SingleInstance();
 {
     builder.RegisterType<ShellSettingsManager>().As<IShellSettingsManager>().SingleInstance();

     builder.RegisterType<ShellContextFactory>().As<IShellContextFactory>().SingleInstance();
     {
         builder.RegisterType<ShellDescriptorCache>().As<IShellDescriptorCache>().SingleInstance();

         builder.RegisterType<CompositionStrategy>().As<ICompositionStrategy>().SingleInstance();
         {
             builder.RegisterType<ShellContainerRegistrations>().As<IShellContainerRegistrations>().SingleInstance();
             builder.RegisterType<ExtensionLoaderCoordinator>().As<IExtensionLoaderCoordinator>().SingleInstance();
             builder.RegisterType<ExtensionMonitoringCoordinator>().As<IExtensionMonitoringCoordinator>().SingleInstance();
             builder.RegisterType<ExtensionManager>().As<IExtensionManager>().SingleInstance();
             {
                 builder.RegisterType<ExtensionHarvester>().As<IExtensionHarvester>().SingleInstance();
                 builder.RegisterType<ModuleFolders>().As<IExtensionFolders>().SingleInstance()
                     .WithParameter(new NamedParameter("paths", new[] { "~/Modules" }));
                 builder.RegisterType<CoreModuleFolders>().As<IExtensionFolders>().SingleInstance()
                     .WithParameter(new NamedParameter("paths", new[] { "~/Core" }));
                 builder.RegisterType<ThemeFolders>().As<IExtensionFolders>().SingleInstance()
                     .WithParameter(new NamedParameter("paths", new[] { "~/Themes" }));

                 builder.RegisterType<CoreExtensionLoader>().As<IExtensionLoader>().SingleInstance();
                 builder.RegisterType<ReferencedExtensionLoader>().As<IExtensionLoader>().SingleInstance();
                 builder.RegisterType<PrecompiledExtensionLoader>().As<IExtensionLoader>().SingleInstance();
                 builder.RegisterType<DynamicExtensionLoader>().As<IExtensionLoader>().SingleInstance();
                 builder.RegisterType<RawThemeExtensionLoader>().As<IExtensionLoader>().SingleInstance();
             }
         }

         builder.RegisterType<ShellContainerFactory>().As<IShellContainerFactory>().SingleInstance();
     }

     builder.RegisterType<DefaultProcessingEngine>().As<IProcessingEngine>().SingleInstance();
 }

3 个答案:

答案 0 :(得分:5)

这种情况下的目标只是,以便开发人员/人员在阅读代码时更容易理解类型之间的层次关系,以便更轻松地理解代码

(使用像这样的花括号将缩进大多数IDE中的行,例如Visual Studio)

答案 1 :(得分:4)

  

请您澄清使用这样的括号的目标是什么?

它们只是块。它们形成了一个范围,如:

Method1(1);

{
   int i = 1;  // i is local to this {} block
   Method1(i++);
}
// Here i does not exist any more

请注意,该块与前一个语句没有任何关系,样本中缺少空行可能会产生误导。

但是在发布的代码中,没有声明变量。所以它们完全是多余的。我猜它们是从一些代码预处理中遗留下来的。

答案 2 :(得分:1)

在这种情况下,它只是一个组织事物。花括号创建一个范围,因此括号内声明的任何变量都是该代码块的本地变量。这就是他们的正常目的。

那就是说,在你给出的例子中,其中任何一个都没有宣布。在这种情况下,Orchard人员只是将其作为一种组织方法来使代码更容易理解。我猜你是否看过你给出的例子中的类,你会发现它们是以分层方式相关的,类似于它们按花括号分组的方式。

相关问题