Đang chuẩn bị liên kết để tải về tài liệu:
Programming C# 4.0 phần 6
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
: Khi định nghĩa một class thừa kế từ DynamicObject, chúng ta vẫn thực hiện các thao tác như việc định nghĩa một class thông thường, có nghĩa là việc định nghĩa các properties, các phương thức là hoàn toàn hợp lệ. Chỉ khác ở một điểm là khi chúng ta truy cập vào các proterties hoặc gọi các methods thì đầu CLR sẽ thực hiện tìm thành phần phù hợp, nếu không tìm thấy thì lúc này DLR mới gọi đến các phương thức mà chúng ta đã override để thực hiện các thao tác mà chúng ta quy. | I warmly recommend that you crank UAC up to the maximum and put up with the occasional security dialog run Visual Studio as a nonadministrator as far as is possible and think at every stage about the least possible privileges you can grant to your users that will still let them get their work done. Making your app more secure benefits everyone not just your own users but everyone who doesn t receive a spam email or a hack attempt because the bad guys couldn t exploit your application. We ve now handled the exception nicely but is stopping really the best thing we could have done Would it not be better to log the fact that we were unable to access particular directories and carry on Similarly if we get a DirectoryNotFoundException or FileNot FoundException wouldn t we want to just carry on in this case The fact that someone has deleted the directory from underneath us shouldn t matter to us. If we look again at our sample it might be better to catch the DirectoryNotFoundExcep tion and FileNotFoundException inside the InspectDirectories method to provide a more fine-grained response to errors. Also if we look at the documentation for FileInfo we ll see that it may actually throw a base lOException under some circumstances so we should catch that here too. And in all cases we need to catch the security exceptions. We re relying on LINQ to iterate through the files and folders which means it s not entirely obvious where to put the exception handling. Example 11-28 shows the code from InspectDirectories that iterates through the folders to get a list of files. We can t put exception handling code into the middle of that query. Example 11-28. Iterating through the directories var allFilePaths from directory in directoriesToSearch from file in Directory.GetFiles directory . searchOption select file However we don t have to. The simplest way to solve this is to put the code that gets the directories into a separate method so we can add exception handling as Example 11-29 .