Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 The .NET Framework Class Library (FCL)

Similar presentations


Presentation on theme: "Chapter 3 The .NET Framework Class Library (FCL)"— Presentation transcript:

1 Chapter 3 The .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 File and Stream I/O Code
StreamReader reader = new StreamReader (filename); for (string line = reader.ReadLine (); line != null; line = reader.ReadLine ()) Console.WriteLine (line); reader.Close ();

5 Collections ArrayList BitArray Hashtable Queue SortedList Stack
Collections (System.Collections) Classes that serve as containers for groups, or collections, of data. ArrayList Resizable arrays BitArray Bit arrays Hashtable Tables of key/value pairs structured for fast lookups Queue First-in, first-out (FIFO) buffers SortedList Tables of sorted key/value pairs accessible by key or index Stack Last-in, first-out (LIFO) buffers

6 Regular Expressions 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 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.)

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). Output format in C#

8 The API that reads metadata for obtaining type information at runtime.
Reflection The API that reads metadata for obtaining type information at runtime. Covered in both classes

9 An Example Assembly: Math.dll
Simple.netmodule Complex.netmodule

10 Reflection 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

11 Reflection Examples 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", " ")] [CodeRevision ("steveb", " ", Comment="Fixed Bill's bugs")] struct Point { public int x; public int y; public int z;}

12 Reflection in C# Tutorial @ CodeProject
Reflection Examples A simple Example Reflection in C# CodeProject The next example loads DLLs; gets type info from the metadata; create objects from the types; gets methods in the types (classes); invokes the methods to create objects of another type.

13 Reflection Examples ArrayList images = new ArrayList (); // to hold “Image” objects 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” Object obj = Activator.CreateInstance (type); //Create a “PlugIn” MethodInfo method = type.GetMethod ("GetImage"); //Get the reference to the text memory of method “GetImage” Image image = (Image) method.Invoke (obj, null); //Invoke the “GetImage” method to create an “Image” object //”null” arguments for GetImage(). images.Add (image); }

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


Download ppt "Chapter 3 The .NET Framework Class Library (FCL)"

Similar presentations


Ads by Google