This analyzer identifies types whose names differ from the file name.
These types can be moved to a separate file (with the same name as the type) if a file contains two or more types. This improves code readability.
C# |
using System;
using System.Collections.Generic;
namespace MySolution
{
public class PluginManager
{
public List<PluginInfo> Plugins { get; private set; }
public void AddPlugin(PluginInfo plugin)
{
if (Plugins == null)
Plugins = new List<PluginInfo>();
Plugins.Add(plugin);
}
}
public class PluginInfo
{
public PluginInfo(string name, string path, bool enabled)
{
Name = name;
Path = path;
Enabled = enabled;
}
public string Name { get; set; }
public string Path { get; set; }
public bool Enabled { get; set; }
}
}
|
VB |
Imports System
Imports System.Collections.Generic
Namespace MySolution
Public Class PluginManager
Public Property Plugins As List(Of PluginInfo)
Public Sub AddPlugin(ByVal plugin As PluginInfo)
If Plugins Is Nothing Then Plugins = New List(Of PluginInfo)()
Plugins.Add(plugin)
End Sub
End Class
Public Class PluginInfo
Public Sub New(ByVal name As String, ByVal path As String, ByVal enabled As Boolean)
name = name
path = path
enabled = enabled
End Sub
Public Property Name As String
Public Property Path As String
Public Property Enabled As Boolean
End Class
End Namespace
|
To fix the issue, move the type declaration to a separate file:
C# |
using System;
using System.Collections.Generic;
namespace MySolution
{
public class PluginManager
{
public List<PluginInfo> Plugins { get; private set; }
public void AddPlugin(PluginInfo plugin)
{
if (Plugins == null)
Plugins = new List<PluginInfo>();
Plugins.Add(plugin);
}
}
}
|
VB |
Imports System
Imports System.Collections.Generic
Namespace MySolution
Public Class PluginManager
Public Property Plugins As List(Of PluginInfo)
Public Sub AddPlugin(ByVal plugin As PluginInfo)
If Plugins Is Nothing Then Plugins = New List(Of PluginInfo)()
Plugins.Add(plugin)
End Sub
End Class
End Namespace
|
C# |
using System;
using System.Collections.Generic;
namespace MySolution
{
public class PluginInfo
{
public PluginInfo(string name, string path, bool enabled)
{
Name = name;
Path = path;
Enabled = enabled;
}
public string Name { get; set; }
public string Path { get; set; }
public bool Enabled { get; set; }
}
}
|
VB |
Imports System
Imports System.Collections.Generic
Namespace MySolution
Public Class PluginInfo
Public Sub New(ByVal name As String, ByVal path As String, ByVal enabled As Boolean)
Name = name
Path = path
Enabled = enabled
End Sub
Public Property Name As String
Public Property Path As String
Public Property Enabled As Boolean
End Class
End Namespace
|
Call the Move Type to File refactoring to move a type declaration to a new source code file.