tailieunhanh - Programming C# 4.0 phần 4

Class DynamicObject cho phép chúng ta định nghĩa các thao tác trên một đối tượng được khai báo với keyword dynamic và các thao tác đó sẽ được thực thi như thế nào. Cụ thể ở đây là chúng ta sẽ không để DLR tự động thực hiện công việc tìm và thực thi một cách tự động các thao tác mà chúng ta sẽ quy định các thao tác gì sẽ xảy ra nếu có truy cập vào các proterties, thực thi methods hoặc gọi các toán tử, . | object of type CalendarEvent shown on the left where each element in the array refers to one of the event objects. CalendarEvent iSwing Dancing at the South Bank Ụpm 11th Jul 2009 4 hrs Saturday Night Swing ự 30pm 11th Jul 2009 hrs rFormula 1 German Grand Prix ụ 12th Jul 2009 3hrsy Swing Dance Picnic Ụpm 12th Jul 2009 4 hrs Stompin at the 100 Club 13th Jul 2009 5 hrs Figure 7-1. An array with reference type elements As you saw in Chapter 3 with reference types multiple different variables can all refer to the same object. Since elements in an array behave in a similar way to local variables of the element type we could create an array where all the elements refer to the same object as shown in Example 7-11. Example 7-11. Multiple elements referring to the same object CalendarEvent theOnlyEvent new CalendarEvent Title Swing Dancing at the South Bank StartTime new DateTimeOffset 2009 7 11 15 00 00 Duration 4 CalendarEvent events theOnlyEvent theOnlyEvent theOnlyEvent theOnlyEvent theOnlyEvent Figure 7-2 illustrates the result. While this particular example is not brilliantly useful in some situations it s helpful for multiple elements to refer to one object. For example imagine a feature for booking meeting rooms or other shared facilities this could be a useful addition to a calendar program. An array might describe how the room will be used today where each element represents a one-hour slot for a particular room. If Arrays 227 the same individual had booked the same room for two different slots the two corresponding array elements would both refer to the same person. Figure 7-2. An array where all of the elements refer to the same object Another feature that reference type array elements have in common with reference type variables and arguments is support for polymorphism. As you saw in Chapter 4 a variable declared as some particular reference type can refer to any object of that type or of any type derived from