Share vb.net extension methods

lamphuong51

New member
#vb.net #Extension Phương thức #.net #Programming #development ## VB.NET Phương thức mở rộng

Các phương thức mở rộng là một tính năng mạnh mẽ của khung .NET cho phép bạn thêm các phương thức mới vào các lớp hiện có mà không phải sửa đổi mã nguồn của chúng.Đây có thể là một cách tuyệt vời để mở rộng chức năng của một lớp mà không phải tạo một lớp có nguồn gốc mới hoặc thêm một hội đồng mới vào dự án của bạn.

Để tạo một phương thức mở rộng, bạn chỉ cần khai báo một phương thức mới có cùng tên với phương thức bạn muốn mở rộng, theo sau là công cụ sửa đổi `exteness`.Chữ ký của phương thức phải khớp với chữ ký của phương thức bạn muốn mở rộng, ngoại trừ tham số đầu tiên phải là một thể hiện của lớp bạn đang mở rộng.

Ví dụ: phương thức tiện ích mở rộng sau đây thêm phương thức `Reverse ()` vào lớp `String`:

`` `VB
Hàm công khai đảo ngược (byval str as chuỗi) dưới dạng chuỗi
Trả lại str.reverse ()
Hàm cuối
`` `

Sau đó, bạn có thể gọi phương thức mở rộng này trên bất kỳ phiên bản `chuỗi` nào, như thế này:

`` `VB
Dim str as String = "Hello World"

Console.WriteLine (str.Reverse ()) 'đầu ra "Dlrow olleh"
`` `

Các phương thức mở rộng là một cách tuyệt vời để thêm chức năng mới vào các lớp hiện có mà không phải sửa đổi mã nguồn của chúng.Chúng cũng là một cách tuyệt vời để chia sẻ mã giữa các dự án, vì bạn có thể chỉ cần nhập cụm có chứa các phương thức mở rộng bạn muốn sử dụng.

###### Tài nguyên bổ sung

* [MSDN: Phương pháp mở rộng] (https://docs.microsoft.com/en-us/dotnet/api/system.reflection.extensionmethod)
* [Stack Overflow: Phương pháp mở rộng] (Reference .NET Assembly from a SQL Server Stored procedure or function)
=======================================
#vb.net #Extension methods #.net #Programming #development ##VB.NET Extension Methods

Extension methods are a powerful feature of the .NET Framework that allow you to add new methods to existing classes without having to modify their source code. This can be a great way to extend the functionality of a class without having to create a new derived class or add a new assembly to your project.

To create an extension method, you simply need to declare a new method with the same name as the method you want to extend, followed by the `Extension` modifier. The method's signature must match the signature of the method you want to extend, except that the first parameter must be an instance of the class you are extending.

For example, the following extension method adds a `Reverse()` method to the `string` class:

```vb
Public Function Reverse(ByVal str As String) As String
Return str.Reverse()
End Function
```

You can then call this extension method on any `string` instance, like this:

```vb
Dim str As String = "Hello World"

Console.WriteLine(str.Reverse()) ' Outputs "dlroW olleH"
```

Extension methods are a great way to add new functionality to existing classes without having to modify their source code. They are also a great way to share code between projects, as you can simply import the assembly that contains the extension methods you want to use.

###### Additional Resources

* [MSDN: Extension Methods](https://docs.microsoft.com/en-us/dotnet/api/system.reflection.extensionmethod)
* [Stack Overflow: Extension Methods](https://stackoverflow.com/questions/136835/what-are-extension-methods-in-c)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top