【问题标题】:Can I split my C# class across multiple files?我可以将我的 C# 类拆分到多个文件中吗?
【发布时间】:2011-10-29 12:56:34
【问题描述】:

我有一个如下所示的课程:

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

}

还有很多我没有在上面展示的。

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

【问题讨论】:

    标签: c#


    【解决方案1】:

    是的,您可以使用partial classes。这允许您将您的课程拆分为多个文件。

    文件 1:

    public static partial class ReferenceData
    {
        /* some methods */
    }
    

    文件 2:

    public static partial class ReferenceData
    {
        /* some more methods */
    }
    

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

    【讨论】:

    • +1。但是,请考虑使用区域在单个文件中更好地组织代码。如果您仍然“需要”几个文件,这通常表明您的课程设计得不好。考虑将其分成几个具有明确定义的工作/行为的类。
    • @JasonWilliams 同意,但请记住,在某些情况下,部分类是必需的,例如在 asp mvc 中,其中一半类是自动生成的,您可能需要添加它。跨度>
    【解决方案2】:

    是的,在您这样做的每个文件的类声明中包含关键字 partial

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

    【讨论】:

    • 部分类必须具有相同的命名空间,只是一个信息。
    【解决方案3】:

    是的,您当然可以,只需在所有声明中在 class 关键字之前使用 partial 关键字。例如,制作 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" }
                };
        }
    }
    

    文件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" }
                };
        }
    }
    

    文件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;
        }
    }
    

    文件4.cs

    public static partial class ReferenceData
    {
    
        // Lots more here
        // Lots more here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-04
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多