MSDN에 정의 되어 있는 내용 입니다.
It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.
둘 이상의 소스 파일에 대해 클래스 또는 구조체, 인터페이스 또는 메소드의 정의를 분할 할 수 있습니다. 각 소스 파일에는 형식 또는 메서드 정의의 섹션이 포함되어 있으며 응용 프로그램이 컴파일 될 때 모든 파트가 결합됩니다.
EnemyAI.cs
public partial class EnemyAI {
public string Init() {
return "Enemy AI Init";
}
}
EnemyAI_Attack.cs
public partial class EnemyAI {
public string Attack() {
return "Enemy Attack!!!!";
}
}
EnemyAI_Death.cs
public partial class EnemyAI {
public string Death()
{
if( HP > 0 )
return "No Die";
return "Enemy Death";
}
}
Program.cs
public class Program {
public static void Main() {
EnemyAI enemy_ai = new EnemyAI();
Console.WriteLine(enemy_ai.Init());
Console.WriteLine(enemy_ai.Attack());
Console.WriteLine(enemy_ai.Death());
}
}
< 정리 >
partial class는 CLR 수준이 아닌 C# Compiler 수준에서 처리되므로 많은 partial class를 만들어도 실제 컴파일시에 하나의 클래스로 수집하여 인식하므로 단일 코드로 취합합니다.
소스코드 수준에서만 허용됩니다. binary 시엔 허용되지 않습니다.
무분별한 partial class 의 사용은 더욱 복잡해질 소지가 있습니다.
여러 파일에 partial 클래스로 선언시 그 클래스의 멤버들을 보는 것은 복잡하겠지만 클래스뷰를 이용하면 partial을 모아서 한 클래스처럼 보실 수 있습니다.
자세한 사항은 https://msdn.microsoft.com/en-us/library/wa80x488.aspx 참고 하시길 바랍니다.
'프로그래밍 > C#' 카테고리의 다른 글
Command Line Reader 클래스 (0) | 2016.11.21 |
---|---|
JsonFx 파일 가독성 높게 저장하기 (0) | 2016.11.21 |