coderushforroslyn-401172-static-code-analysis-analyzers-library-crr0047-type-can-be-moved-to-a-separate-file.md
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.
//PluginManager.cs
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; }
}
}
'PluginManager.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:
//PluginManager.cs
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);
}
}
}
'PluginManager.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
//PluginInfo.cs
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; }
}
}
'PluginInfo.cs
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.
See Also