Lập trình .net 4.0 và visual studio 2010 part 13 docx

5 332 0
Lập trình .net 4.0 và visual studio 2010 part 13 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 4  CLR AND BCL CHANGES 83 var key = instance.CreateSubKey(subkey, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile); In 64-bit versions of Windows, data is stored separately in the registry for 32- and 64-bit applications. OpenBaseKey()() and OpenRemoteBaseKey() methods now allow you to specify a new enum called RegistryView for specifying the mode that should be used when reading. Note that if you mistakenly use the 64-bit option on a 32-bit version, you will get the 32-bit view. Stream.CopyTo() Stream.CopyTo() allows you to copy the contents of one stream to another, avoiding some tedious coding: MemoryStream destinationStream = new MemoryStream(); using (FileStream sourceStream = File.Open(@"c:\temp.txt", FileMode.Open)) { sourceStream.CopyTo(destinationStream); } Guid.TryParse(), Version.TryParse(), and Enum.TryParse<T>() New TryParse() methods have been added to Guid, Version and Enum types: Guid myGuid; Guid.TryParse("not a guid", out myGuid); Enum.HasFlag() Returns a Boolean value indicating whether one or more flags are set on an enum. For example, you could use the HasFlag() method to test whether a car has particular options set: [Flags] public enum CarOptions { AirCon = 1, Turbo = 2, MP3Player = 4 } static void Main(string[] args) { CarOptions myCar = CarOptions.MP3Player | CarOptions.AirCon | CarOptions.Turbo; Console.WriteLine("Does car have MP3? {0}", myCar.HasFlag(CarOptions.MP3Player)); Console.ReadKey(); } CHAPTER 4  CLR AND BCL CHANGES 84 String.Concat() and String.Join() support IEnumerable<T> New overloads allow the concatenation and joining of IEnumerable elements without having to convert them to strings first, which can make LINQ queries cleaner. String.IsNullOrWhiteSpace() Detects if a string is null, empty, or consists of whitespace characters (avoiding a call to Trim()): String.IsNullOrWhiteSpace(" "); This is one of my favorite changes. It is such a common thing to do that it is great to have it baked into the framework. StringBuilder.Clear Removes content from the StringBuilder object (essentially the same as setting a string builder’s length to 0, but with a more readable syntax): StringBuilder sb = new StringBuilder("long string"); sb.Clear(); Note that calling StringBuilder.Clear() does not reset the MaxCapacity property. Environment.SpecialFolder Enum Additions The SpecialFolder enum has had a number of new options added to represent pretty much any type of folder on a Windows machine. For example, you can access the CDBuring folder: Environment.SpecialFolder.CDBurning Environment.Is64BitProcess and Environment.Is64BitOperatingSystem 64-bit is becoming mainstream now, so new methods have been added to return a Boolean value, indicating whether your application or process is running in a 64-bit process or on a 64-bit system: Console.WriteLine(Environment.Is64BitOperatingSystem); Console.WriteLine(Environment.Is64BitProcess); Stopwatch.Restart() Stops the recording of the current time period and starts a new one: var sw = new Stopwatch(); sw.Start(); sw.Restart(); CHAPTER 4  CLR AND BCL CHANGES 85 ServiceProcessInstaller.DelayedAutoStart .NET services running on Vista or above can make use of the ServiceProcessInstaller.DelayedAutoStart feature to delay their start until other autostart services have already started. This can reduce the boot time for users. Observable collection refactoring ObservableCollection<T>, ReadOnlyObservableCollection<T>, and System.Collections.Specialized. INotifyCollectionChanged have been moved into System.dll because they were useful outside of WPF applications. This is great news because you no longer have to reference WPF assemblies. IObservable<T> An interface for implementation of the observer pattern. Network Class Libraries (NCLs) All classes in the System.Net namespace have improved stability and performance. Standards compliance has been improved for FTP, HTTP, SMTP, and URIs with better support for internationalization.  NOTE At the time of writing, little information is available about these changes, so my apologies for the lack of detail. IPv6 Support Applications running on Vista onward can now handle multiple versions of the IP protocol (v4 & v6) by setting the IPv6Only option to false when creating the socket. HttpWebRequest HttpWebRequest has had two new properties added: • Date for setting HTTP date header • Host for setting HTTP host header (this can be useful for testing load balancing scenarios to test connection to a particular server) Examples of these properties are as follows: string loadbalancerIp = "http://127.0.0.1/"; string host = "mywebsite.com"; var request = WebRequest.Create("http://127.0.0.1/") as HttpWebRequest; request.Date = System.DateTime.Now; Support has also been added in HttpWebRequest.AddRange()() for int64 ranges. CHAPTER 4  CLR AND BCL CHANGES 86 DNS Endpoint DnsEndPoint is a new class that allows you to create a socket connection by specifying a FQDN name without making a call to Dns.GetHostAddresses()() resulting in reduced and clearer code: var socket = new System.Net.Sockets.Socket(new System.Net.Sockets.SocketInformation()); socket.Connect(new DnsEndPoint("www.microsoft.com", 80)); Default SSL policy For interoperability, it is sometimes necessary to turn off SSL encryption (a null cipher). HttpWebRequest, FtpWebRequest, and SmtpClient now allow you to use a null cipher by specifying it using the System.Net.Security.EncryptionPolicy enum or in Web/machine.config: SMTP Client The SMTP client contains a number of useful enhancements: • Enabling SSL mode in application config files • Specifying heading encoding • Multiple replying to addresses through MailMessage.ReplyToList() TCPListener Support for NAT Transversal Many applications are located behind a firewall, which can cause complications when communicating with them. One way around this is network address translation (NAT) transversal, which takes care of various complications. In .NET 4.0, NAT transversal support has been added to TcpListener and UdpClient. WebRequest New performance counters have been added for WebRequest: • HttpWebRequests created/sec • HttpWebRequests queued/sec • HttpWebRequests aborted/sec • HttpWebRequests failed/sec • HttpWebRequest average lifetime • HttpWebRequests' average queue time You can review the diagram shown here to see where they occur in a request lifetime: http://blogs. msdn.com/blogfiles/ncl/WindowsLiveWriter/NewNCLFeaturesin.NET4.0Beta2_78A0/image_2.png. CHAPTER 4  CLR AND BCL CHANGES 87 Windows 7 Only .NET 4.0 has some new features that are available only for Windows 7 machines. System.Device.Location .NET 4.0 contains a new API for querying Windows 7’s location functionality. The Location class supports multiple devices such as GPS and wireless wide area network (WWANwireless by cellular phone technology). It returns the current latitude, longitude, altitude, horizontal and vertical accuracy, course, speed, and civic address (country/region, state/province, city, postal code, street, building, and floor level, if available). At the time of writing, the NCL team is refactoring these libraries. ExtendedProtection Windows 7 introduces enhanced security features to prevent replay network attacks. Classes in System.Net and related namespaces such as HttpWebListener, HttpWebRequest, NegotiateStream, SmtpClient, and SSLStream will now utilize these enhancements by default when Windows authentication is used. For the nitty-gritty full details of these changes, consult http://msdn. microsoft.com/en-us/library/dd582691(VS.100).aspx. Deprecated APIs A number of existing important APIs are now marked as deprecated. A full list is available at the following: • http://msdn.microsoft.com/en-au/library/ee461503(VS.100).aspx (Types) • http://msdn.microsoft.com/en-au/library/ee471421(VS.100).aspx (Members) It is also worth noting two commonly used APIs that are now deprecated, as discussed in the next two sections. System.Data.OracleClient System.Data.OracleClient is available in .NET 4.0, but is marked as deprecated. Microsoft says this is because most developers use partner company Oracle providers. Microsoft will continue to issue hotfixes for critical OracleClient issues. For more info, refer to http://blogs.msdn.com/adonet/archive/ 2009/06/15/system-data-oracleclient-update.aspx. Global Static Hosting Functions Global static hosting functions have now been deprecated. For more info, please refer to http:// msdn.microsoft.com/en-us/library/aa964945(VS.100).aspx. . msdn.com/blogfiles/ncl/WindowsLiveWriter/NewNCLFeaturesin .NET4 .0Beta2_78A0/image_2.png. CHAPTER 4  CLR AND BCL CHANGES 87 Windows 7 Only .NET 4. 0 has some new features that are available only for. following: • http://msdn.microsoft.com/en-au/library/ee461 503 (VS. 100 ).aspx (Types) • http://msdn.microsoft.com/en-au/library/ee47 142 1(VS. 100 ).aspx (Members) It is also worth noting two commonly used. communicating with them. One way around this is network address translation (NAT) transversal, which takes care of various complications. In .NET 4. 0, NAT transversal support has been added to

Ngày đăng: 01/07/2014, 21:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan