July 21, 2014

Partial Classes

มีหลายเหตุที่ต้องแยก class (partial class) ได้ตามความซับซ้อนตามที่ต้องการ:
  • เมื่อทำงานกับโปรเจ็คขนาดใหญ่, การแยก class ไว้ในหลายไฟล์เพื่อให้โปรแกรมเมอร์สามารถทำงานกับ class นั้นได้ในเวลาเดียวกัน.
  • เมื่อทำงานกับการสร้าง code โดยโปรแกรม (automatically generated source), code สามารถถูกเพิ่มเข้าไปใน class โดยไม่ต้องสร้าง source file ใหม่. Visual Studio ใช้วิธีการนี้เมื่อใช้สร้าง Windows Forms, Web service wrapper code, and so on. คุณสามารถสร้าง code ที่ใช้งาน class โดยไม่ต้องแก้ไขาไฟล์ที่ถูกสร้างโดย Visual Studio.
  • แยกหนึ่งที่มีความซับซ้อน, ให้ใช้ modifier keyword เป็น partial , ดังเช่นด้านล่าง:
public partial class Employee
{
    public void DoWork()
    {
    }
}

public partial class Employee
{
    public void GoToLunch()
    {
    }
}
partial keyword แสดงให้เห็นว่าส่วนอื่น ๆ ของ class, struct, หรือ interface สามารถสร้างเพิ่มได้ใน namespace. ทุกอยางภายในต้องใช้ partial keyword. ทุกอย่างภายในต้อง available ตอน compile time ในรูปแบบ final type. ทุกส่วนต้องมี accessibility ที่เหมือนกัน, เช่น publicprivate, และอื่น ๆ.
ถ้ามีบางส่วน declared เป็น abstract, นั้นทำให้ทั้งหมดเป็น abstract. ถ้าหลาย ๆ ส่วนกำหนดเป็น sealed, นั้นทำให้ทั้งหมดของ type ที่มีอยู่เป็น sealed. ถ้าทุกส่วนของกำหนดเป็น base type, นั้นทำให้ทั้งหมดของของ type ที่ inherits จาก class ทั้งหมด.


Note Note
partial modifier ไม่สามรถใช้งานร่วมกับ delegate หรือ enumeration declarations.
ด้านล่างแสดง nested types ที่สามารถเป็น partial, ถ้า type มันเป็น nested ภายในจะไม่มี partial อยู่ด้านใน.
class Container
{
    partial class Nested
    {
        void Test() { }
    }
    partial class Nested
    {
        void Test2() { }
    }
}
ตอน compile time, attributes ของ partial-type ทั้งหมดจะถูกรวมเข้าด้วยกัน. จากตัวอย่าง, พิจารณาตามลำดับ declarations:
[SerializableAttribute]
partial class Moon { }

[ObsoleteAttribute]
partial class Moon { }
มีค่าเท่ากับการ declarations ด้านล่าง:
[SerializableAttribute]
[ObsoleteAttribute]
class Moon { }
ด้านล่างได้ถูกรวมไว้เป็น partial-type definitions:
  • XML comments
  • interfaces
  • generic-type parameter attributes
  • class attributes
  • members
จากตัวอย่าง, พิจารณาจากลำดับการ declarations:
partial class Earth : Planet, IRotate { }
partial class Earth : IRevolve { }
มีค่าเท่ากับการ declarations ด้านล่าง:
class Earth : Planet, IRotate, IRevolve { } 

ข้อจำกัด (Restrictions)


There are several rules to follow when you are working with partial class definitions:
  • All partial-type definitions meant to be parts of the same type must be modified with partial. For example, the following class declarations generate an error:
    public partial class A { }
    //public class A { }  // Error, must also be marked partial
    
  • The partial modifier can only appear immediately before the keywords classstruct, or interface.
  • Nested partial types are allowed in partial-type definitions as illustrated in the following example:
    partial class ClassWithNestedClass
    {
        partial class NestedClass { }
    }
    
    partial class ClassWithNestedClass
    {
        partial class NestedClass { }
    }
    
  • All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.
  • The class name and generic-type parameters must match on all partial-type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.
  • The following keywords on a partial-type definition are optional, but if present on one partial-type definition, cannot conflict with the keywords specified on another partial definition for the same type:







อ่านทั้งหมด: http://msdn.microsoft.com/en-us/library/wa80x488.aspx

No comments:

Post a Comment