2007年11月24日星期六

What's New in .NET Framework 3.5 - Extension Methods 延伸方法

English:
With traditional object-oriented development, extending classes is typically achieved by deriving from a base class and enhancing the functionality in the derived class. Visual BasicR still supports these object-oriented concepts, but classes are sometimes marked NotInheritable to prevent modification of their behavior through inheritance. As a result, there is no way to customize these classes. An example of this is the System.String class.

A new feature made available in Visual Basic 2008, however, lets you extend any existing type's functionality, even when a type is not inheritable. And these extension methods play a crucial role in the implementation of LINQ.

http://msdn.microsoft.com/msdnmag/issues/07/11/BasicInstincts/default.aspx?loc=en

繁體中文:
在傳統的物件導向開發架構中,類別的延伸通常涉及以基底類別進行衍生,然後在衍生類別中增強功能。Visual Basic 依然支援這些物件導向的概念,但是類別有時候會標示為 NotInheritable,以防止其行為透過繼承而被修改。於是便沒有自訂類別的方法。如此的範例包括 System.String 類別。

但是,在 Visual Basic 2008 中的新功能,可以讓您延伸任何現有型別的功能,即使該型別為不可繼承的型別。而且這些延伸方法在 LINQ 的實作過程中擔任著關鍵的角色。

許多現有的型別都無法在不中斷現有程式碼的情況下輕易更新。如此的範例包括 IEnumerable(Of T) 介面。為了要支援 LINQ,介面中必須加入新的方法,但是加入新的方法對於介面的改變卻會中斷現有消費者的相容性。加入新的介面也是一個可能性,但是建立新的介面來補充現有的 IEnumerable(Of T) 介面,在設計上會變得奇怪。

此情況所需的是延伸現有型別的方法,讓型別可以在不變更現有合約的前提下新增功能。
http://msdn.microsoft.com/msdnmag/issues/07/11/BasicInstincts/default.aspx?loc=zx

沒有留言:

發佈留言