2010-02-09 34 views
23

Tôi đang cố gắng thêm chức năng tùy chỉnh vào lớp ImageMagickNet. Nó nên sử dụng phương thức IsSimilarImage magick từ dự án ImageMagick.NET nhưng tôi nhầm lẫn là liệu tôi có phải định tuyến phương thức này thông qua Magick ++ hay không, vì mọi chức năng có sẵn cho phía .NET bắt nguồn từ Magick ++.Mở rộng ImageMagickNet

Trả lời

2

Điều này là khá cũ nhưng như nó chưa được trả lời, ở đây đi.

Xin lưu ý rằng tôi chưa xem các thư viện ImageMagick, vì vậy bất kỳ chi tiết triển khai nào trong mã bên dưới đều là một ví dụ. Thay thế rác bằng cách thực hiện chính xác. Giả sử nó đang xuất các đối tượng .NET hợp lệ, đây là cách nó hoạt động:

' Put your extension methods or properties in a clearly labeled module file, on its own within your project 
Module ImageMagickNetExtensions 

    ' Define an extension method by using the ExtensionAttribute, and make the first argument 
    ' for the method the type that you wish to extend. This will serve as a reference to the extended 
    ' instance, so that you can reference other methods and properties within your extension code. 
    <Extension()> _ 
    Public Function SomeExtensionFunction(ByVal imn As ImageMagickNet, ByVal filename As String) As Boolean 
     Return imn.IsSimilarImage(filename) 
    End Function 

End Module 

Class SomeClass 
    ' To use your extension method within your project containing the extension module, simply 
    ' call it on any valid instance of the type you have extended. The compiler will call your code 
    ' whenever it sees reference to it, passing a reference to your extended instance. 
    Private imn As New ImageMagickNet 

    Private Sub DoSomething() 
     If imn.SomeExtensionFunction("c:\someimage.jpg") Then 
      ... 
     End If 
    End Sub 
End Class