System.AddIn - Part 0: Terminology and Resource

Posted by Mr.NanhTrang on August 30th, 2008

Extensibility Terminology

  • Host An application that supports extensibility. A host may be a client or server (for example, Excel, Word, Paint.NET, SQL Server, and Exchange Server).
  • Extensibility A mechanism for manipulating host application objects or extending host functionality, sometimes referred to as automation. Generally made available via an object model published as part of a host’s SDK
  • Software Development Kit (SDK) A set of public interfaces (APIs) and associated documentation. Allows the host application to provide services to an add-in and vice versa. This SDK may be provided by different parties in different situations. For example, the host may provide the SDK to enable host-specific add-ins, an add-in may provide the SDK to let many different hosts use it (for example, Windows Media® Player), a third party may provide the SDK to let many different hosts, and add-ins communicate with each other.
  • Add-In (also called Add-On, Extension, Plug-In, Snap-In) A customization; an assembly loaded by a host. Also, something that automates a host application (such as a client). Something that provides additional functionality to a host (service). An add-in is custom code, usually written by a third party, that is dynamically loaded and activated, by a host application, based upon some context (such as host startup or document loading). The add-in extends the host application via the host’s public API (for example, an object model or managed class library) which was made available to the add-in via an SDK.
  • Pipeline The machinery that enables the host and add-in to communicate over a version-resilient, secure protocol.

Resource

  1. CLR Inside Out: .NET Application Extensibility part 1 & part 2 : The first pages you should read when study System.AddIn namespace.
  2. CLR Add-In Team Blog: All about the CLR’s System.AddIn model for managed add-ins, add-ons, plug-ins, and extensions. Where you can study, discussion and understand some intenal implement of System.AddIn.
  3. System.AddIn Tools and Samples: Where you can download tools and samples. Post a question or discussion about System.AddIn.

How to use Extension Methods on .Net 2.0

Posted by Mr.NanhTrang on June 24th, 2008

Extension Method là điểm mới của .NET 3.0+, nếu bạn sử dụng trong .NET 2.0 với Visual Studio thì bạn sẽ được “tặng” lỗi liền :

Extension Methods on .Net 2.0 - error

Extension Methods sau khi compile thì thực chất chỉ là static methods, tuy nhiên để giúp IDE (Visual Studio) có thể nhận biết được static methods nào là extension methods để cho IDE có thể biết được kiểu dữ liệu nào có extension method nào (giúp IDE có list ra các hàm, cũng như không báo lỗi và compile được khi viết theo cú pháp của extension methods), compiler đã thêm attribute: ExtensionAttribute cho các static method (cũng như static class và assembly chứa nó) là extension methods. Thật không may, cái attributes này lại nằm trong System.Core.dll component (.Net 3.5 - cũng phải thôi, M$ không thể thêm cái attribute mới này vào trong các component đã được publish của .Net 2.0 được), vì thế khi chúng ta sử dụng Extension Methods cho project .net 2.0 sẽ nhận được thông báo lỗi như trên.

Read the rest of this entry »

Extension Methods trong .NET 3.5

Posted by Nguyễn Hùng Cường on June 15th, 2008

Vấn đề

Như các bạn đã biết thì khi ta muốn thêm một method nào đó vào một class thì bạn phải sửa code và biên dịch lại assembly chứa class đó. Rõ ràng điều này chỉ “tạm chấp nhận” khi code đó chính là do bạn tạo ra, do bạn giữ bản quyền chứ không thể khi ta sử dụng các library của người khác.

Thật ra, nếu nói cho đúng thì có 2 cách mà bạn có thể workaround cái vấn đề này :

  • Thứ nhất. Inherit cái class bạn muốn thêm method
  • Thứ hai. Tạo một static class mới với đầu vào là class muốn thêm method và đầu ra là class sau khi bạn đã thao tác trong method này. (thường thấy mọi người hay dùng kiểu này)

Tuy nhiên chúng ta hãy xem xét các mặt không có lợi khi áp dụng các phương thức trên :

  • Phương thức 1 : Vấn đề xảy ra khi class bạn muốn thêm method không cho inherit. Tiêu biểu là sealed classes như String, Int16, Int32, … Hơn nưã giả sử inherit được thì nó lại gây khó khăn khi sử dụng, ví dụ như thay vì dùng một cách tự nhiên là String thì bạn phải sử dụng MyString, ModedString, …. Không hề tự nhiên.
  • Phương thức 2 : Có thể tạm chấp nhận được nhưng nó đi ngược với tư tưởng hướng đối tượng, cách viết method sẽ trở về “nguyên thủy” khi làm việc với struct, function, … Bạn sẽ phải String x = MyClass.Modify(inputString) thay vì x = x.Modify()

Và điều này đã được giải quyết khá gọn gàng trong .NET 3.5 (thực ra là từ .NET 3.0) với Extenstion Methods
Read the rest of this entry »




Wordpress Theme by Justskins
Copyright © 2007 ExD Sandbox. All rights reserved.