我可以在多个文件中拆分C#类吗?

Sam*_*tar 11 c#

我有一个看起来像这样的课程:

public static class ReferenceData
{

    public static IEnumerable<SelectListItem> GetAnswerType()
    {
        return new[]
            {
                new SelectListItem { Value = "1", Text = "1 answer"  },
                new SelectListItem { Value = "2", Text = "2 answers" },
                new SelectListItem { Value = "3", Text = "3 answers" }
            };
    }

    public static IEnumerable<SelectListItem> GetDatastore()
    {
        return new[]
            {
                new SelectListItem { Value = "DEV", Text = "Development"  },
                new SelectListItem { Value = "DC1", Text = "Production" }
            };
    }
    public static string GetDatastoreText(string datastoreValue)
    {
        return GetDatastore().Single(s => s.Value == datastoreValue).Text;
    }
    public static string GetDatastoreValue(string datastoreText)
    {
        return GetDatastore().Single(s => s.Text == datastoreText).Value;
    }

    // Lots more here
    // Lots more here

}
Run Code Online (Sandbox Code Playgroud)

我上面没有展示更多内容.

目前,所有类信息都在一个文件中.但是,我想将其拆分为多个文件.有没有什么方法可以跨多个文件传播ReferenceData类的内容?

Mar*_*ers 30

是的,您可以使用部分课程.这允许您将类拆分为多个文件.

档案1:

public static partial class ReferenceData
{
    /* some methods */
}
Run Code Online (Sandbox Code Playgroud)

文件2:

public static partial class ReferenceData
{
    /* some more methods */
}
Run Code Online (Sandbox Code Playgroud)

请谨慎使用此功能.过度使用会使代码难以阅读.

  • @JasonWilliams同意但请记住,有些情况下需要部分类,例如在asp mvc中,其中一半是自动生成的,并且您可能想要添加它. (2认同)

Tes*_*rex 26

是的,partial在您执行此操作的每个文件的类声明中包含关键字.

http://msdn.microsoft.com/en-us/library/wa80x488.aspx

  • 局部类必须具有相同的名称空间,只是一个信息。 (2认同)

pix*_*ker 7

是的,您当然可以,只需在所有声明的partial关键字之前使用class关键字。例如,制作 4 个不同的文件(但在同一个命名空间中)包含类的方法和成员,ReferenceData如下所示:

文件1.css

public static partial class ReferenceData
{

    public static IEnumerable<SelectListItem> GetAnswerType()
    {
        return new[]
            {
                new SelectListItem { Value = "1", Text = "1 answer"  },
                new SelectListItem { Value = "2", Text = "2 answers" },
                new SelectListItem { Value = "3", Text = "3 answers" }
            };
    }
}
Run Code Online (Sandbox Code Playgroud)

文件2.cs

public static partial class ReferenceData
{

    public static IEnumerable<SelectListItem> GetDatastore()
    {
        return new[]
            {
                new SelectListItem { Value = "DEV", Text = "Development"  },
                new SelectListItem { Value = "DC1", Text = "Production" }
            };
    }
}
Run Code Online (Sandbox Code Playgroud)

文件3.cs

public static partial class ReferenceData
{

    public static string GetDatastoreText(string datastoreValue)
    {
        return GetDatastore().Single(s => s.Value == datastoreValue).Text;
    }
    public static string GetDatastoreValue(string datastoreText)
    {
        return GetDatastore().Single(s => s.Text == datastoreText).Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

文件4.cs

public static partial class ReferenceData
{

    // Lots more here
    // Lots more here
}
Run Code Online (Sandbox Code Playgroud)