我有一个看起来像这样的课程:
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)
请谨慎使用此功能.过度使用会使代码难以阅读.
Tes*_*rex 26
是的,partial在您执行此操作的每个文件的类声明中包含关键字.
http://msdn.microsoft.com/en-us/library/wa80x488.aspx
是的,您当然可以,只需在所有声明的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)
| 归档时间: |
|
| 查看次数: |
19573 次 |
| 最近记录: |