Effective C#50 Specific Ways to Improve Your C# 2nd phần 9

kiểm tra thời gian chạy có thể được sắp xếp hợp lý, mà kết quả trong hiệu suất tốt hơn. C # là một ngôn ngữ tĩnh đánh máy và sẽ vẫn là một. Tuy nhiên, đối với những lần khi các ngôn ngữ động cung cấp các giải pháp hiệu quả hơn, C # có tính năng động. Những tính năng cho phép bạn chuyển đổi giữa gõ tĩnh | 260 I Chapter 5 Dynamic Programming in C from srcProp in typeof TSource .GetProperties where The let declares a local variable that holds the property of the same name in the destination type. It may be null if the destination type does not have a property of the correct type let destProp typeof TDest .GetProperty where destProp null The projection of the query is a sequence of assignment statements that assigns the property of the destination object to the value of the same property name in the source object select dest destProp source srcProp The rest of the method builds the body of the lambda expression. The Block method of the Expression class needs all the statements in an array of Expression. The next step is to create a List Expression where you can add all the statements. The list can be easily converted to an array. var body new List Expression dest typeof TDest assignments dest Finally it s time to build a lambda that returns the destination object and contains all the statements built so far var expr Func TSource TDest new dest expression parameters body source lambda expression From the Library of Wow eBook Item 43 Use Expressions to Transform Late Binding into Early Binding I 261 That s all the code you need. Time to compile it and turn it into a delegate that you can call var func converter func That is complicated and it s not the easiest to write. You ll often find compiler-like errors at runtime until you get the expressions built correctly. It s also clearly not the best way to approach simple problems. But even so the Expression APIs are much simpler than their predecessors in the Reflection APIs. That s when you should use the .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
110    89    5    04-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.