170 likes | 372 Views
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.
E N D
Chapter 3The .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. • Stream classes have methods that you can call to perform input and output. • An additional level of abstraction: readers and writers.
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.
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
Collections (System.Collections) Classes that serve as containers for groups, or collections, of data. Collections
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
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).
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
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
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
FCL is the API for managed applications. Most commonly used namespaces: IO, Collections, Net, Data, Reflection. Read and practice the details. Summary