1 / 11

Chapter 3 The .NET Framework Class Library (FCL)

Chapter 3 The .NET Framework Class Library (FCL). Yingcai Xiao. File and Stream I/O. FCL provides the API that managed applications write to. 100 hierarchically organized namespaces and more than 7,000 types. File and Stream I/O A stream is an abstract representation of byte-oriented data.

fuller
Download Presentation

Chapter 3 The .NET Framework Class Library (FCL)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3The .NET Framework Class Library (FCL) Yingcai Xiao

  2. File and Stream I/O • FCL provides the API that managed applications write to. • 100 hierarchically organized namespaces and more than 7,000 types. • File and Stream I/O • A stream is an abstract representation of byte-oriented data. • Stream classes have methods that you can call to perform input and output. • An additional level of abstraction: readers and writers.

  3. File and Stream I/O • General Procedure • 1. Open the file using a FileStream object. • 2. For binary reads and writes, wrap instances of BinaryReader and BinaryWriter around the FileStream object and call BinaryReader and BinaryWriter methods such as Read and Write to perform input and output. • 3. For reads and writes involving text, wrap a StreamReader and StreamWriter around the FileStream object and use StreamReader and StreamWriter methods such as ReadLine and WriteLine to perform input and output. • 4. Close the FileStream object.

  4. StreamReader reader = new StreamReader (filename); for (string line = reader.ReadLine (); line != null; line = reader.ReadLine ()) Console.WriteLine (line); reader.Close (); File and Stream I/O Code

  5. Collections (System.Collections) Classes that serve as containers for groups, or collections, of data. Collections

  6. Regex: regular expressions, a language for parsing and manipulating text. Regex supports three basic types of operations: Splitting, Searching, Replacing. Regex regex = new Regex (@"\\"); // use “\” as a delimiter. string[] parts = regex.Split (@"c:\inetpub\wwwroot\wintellect"); foreach (string part in parts) Console.WriteLine (part); c: inetpub wwwroot wintellect (@: no need of \\ for the expression to be parsed. \ is an escape character, e.g., \n. \\ means to treat \ as a regular character.) http://en.wikipedia.org/wiki/Regular_expression Regular Expressions

  7. Internet Classes HttpWebRequest and HttpWebResponse (System.Net) LinkList.cs System.Web.Mail: MailMessage, MailAttachment and SmtpMail • Data Access (System.Data) (More in Chapter 12) ADO.NET is .NET Framework’s API for database access. SqlDataReader (System.Data.SqlClient) reads data from Microsoft SQL Server databases only (optimized for it). OleDbDataReader (System.Data.OleDb) reads data from all types of databases (slower than SqlDataReader, but portable).

  8. Reflection (System.Reflection) • Managed applications stores metadata in assemblies and modules. • • Retrieving information about assemblies and modules and the types they contain • • Reading information added to an executable’s metadata by custom attributes • • Performing late binding by dynamically instantiating and invoking methods on types • Useful classes are in • • System.Reflection.Assembly, which represents assemblies • • System.Reflection.Module, which represents managed modules • • System.Type, which represents types Reflection

  9. Retrieving Info (AsmInfo.cs): Assembly a = Assembly.LoadFrom ("Math.dll"); Assembly methods: GetModules, GetExportedTypes, GetReferencedAssemblies, • Custom Attributes (System.Attribute) Attributes are a declarative means for adding information to metadata. [Conditional ("DEBUG")] public DoValidityCheck () { ... } • CodeRevision attribute [CodeRevision ("billg", "07-19-2001")] [CodeRevision ("steveb", "09-30-2001", Comment="Fixed Bill's bugs")] struct Point { public int x; public int y; public int z;} ReflectionExamples

  10. Dynamically Loading Types Late Binding: binding to a type at run time rather than compile time. // Each file below contains an image and related code to process it. ArrayList images = new ArrayList (); foreach (string name in names) {//names: an array of file names Assembly a = Assembly.Load (name); //load the file Type type = a.GetType ("PlugIn"); //get the class “PlugIn” MethodInfo method = type.GetMethod ("GetImage"); Object obj = Activator.CreateInstance (type); //Create a “PlugIn” Image image = (Image) method.Invoke (obj, null); images.Add (image); } ReflectionExamples

  11. FCL is the API for managed applications. Most commonly used namespaces: IO, Collections, Net, Data, Reflection. Read and practice the details. Summary

More Related