Back to Devexpress

CRR0047 - The type can be moved to a separate file

coderushforroslyn-401172-static-code-analysis-analyzers-library-crr0047-type-can-be-moved-to-a-separate-file.md

latest4.0 KB
Original Source

CRR0047 - The type can be moved to a separate file

  • Feb 28, 2025
  • 3 minutes to read

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.

csharp
//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; }
    }
}
vb
'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:

csharp
//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);
        }
    }
}
vb
'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
csharp
//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; }
    }
}
vb
'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

Suppress Analyzers