Essential C# 3.0 FOR NET FRAMEWORK 3.5 PHẦN 10

yêu cầu các thông số bằng cách chèn mặc định cho các cuộc gọi phương pháp extern. • Sử dụng enum hoặc const để cung cấp các giá trị liên tục cho các API như một phần củahương 20: Khả năng cộng tác Platform và Mã không an toàn • Cung cấp các phương pháp wrapper công cộng xung quanh các phương pháp bên ngoài | 738 Chapter 20 Platform Interoperability and Unsafe Code Provide public wrapper methods around the external methods that handle the data type conversions and error handling. Overload the wrapper methods and provide a reduced number of required parameters by inserting defaults for the extern method call. Use enum or const to provide constant values for the API as part of the API s declaration. For all P Invoke methods that support GetLastError be sure to assign the SetLastError named attribute to true. This allows the reporting of errors via . Wrap resources such as handles into classes that derive from or that support IDis-posable. Function pointers in unmanaged code map to delegate instances in managed code. Generally this requires the declaration of a specific delegate type that matches the signature of the unmanaged function pointer. Map input output and output parameters to ref parameters instead of relying on pointers. The last bullet implies C s support for pointers described in the next section. Pointers and Addresses On occasion developers will want to be able to access and work with memory and with pointers to memory locations directly. This is necessary for certain operating system interaction as well as with certain types of time-critical algorithms. To support this C requires use of the unsafe code construct. Unsafe Code One of C s great features is that it is strongly typed and supports type checking throughout the runtime execution. What makes this feature especially great is that it is possible to circumvent this support and manipulate memory and addresses directly. You would do this when working with things such as memory-mapped devices or if you wanted Pointers and Addresses 739 to implement time-critical algorithms. The key is to designate a portion of the code as unsafe. Unsafe code is an explicit code block and compilation option as shown in Listing . The unsafe

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
192    105    8    02-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.