Rediģēt

Kopīgot, izmantojot


Namespace does not match folder structure (IDE0130)

Property Value
Rule ID IDE0130
Title Namespace does not match folder structure
Category Style
Subcategory Language rules (expression-level preferences)
Applicable languages C# and Visual Basic
Options dotnet_style_namespace_match_folder

Overview

This style rule uses the folder structure of the project to enforce namespace naming requirements.

Options

Options specify the behavior that you want the rule to enforce. For information about configuring options, see Option format.

dotnet_style_namespace_match_folder

Property Value Description
Option name dotnet_style_namespace_match_folder
Option values true Prefer namespace naming to match folder structure.
false Disables the rule.
Default option value true

Note

The dotnet_style_namespace_match_folder option depends on knowing the current project and root namespace properties. This information is provided by Visual Studio but is not available for command-line builds, such as dotnet build. For command-line builds to work, you must add the following properties to your project file:

<ItemGroup>
  <CompilerVisibleProperty Include="RootNamespace" />
  <CompilerVisibleProperty Include="ProjectDir" />
</ItemGroup>

Example

Assume that the following code snippets are from a file named Data/Example.cs or Data/Example.vb, where Data represents the folder structure from the project file. The folder structure naming is added to the root namespace, which in this example is Root.

// Code with violations
namespace Root.BadExample
{
    class Example
    {
        public void M()
        {
        }
    }
}

// Fixed code
namespace Root.Data
{
    class Example
    {
        public void M()
        {
        }
    }
}
' Code with violations
Namespace Root.BadExample
    Class Example
        Public Sub M()
        End Sub
    End Class
End Namespace

' Fixed code
Namespace Root.Data
    Class Example
        Public Sub M()
        End Sub
    End Class
End Namespace

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable IDE0130
// The code that's violating the rule is on this line.
#pragma warning restore IDE0130

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.IDE0130.severity = none

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

For more information, see How to suppress code analysis warnings.

See also