1 / 112

Organizational Communications and Distributed Object Technologies

Organizational Communications and Distributed Object Technologies. Lecture 12: .NET. Overview. Basics. .NET. Runtime environment called the Common Language Runtime (CLR) Class library called the Framework Class Library (FCL). Common Language Runtime. Modern runtime environment

hullm
Download Presentation

Organizational Communications and Distributed Object Technologies

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. Organizational Communications and Distributed Object Technologies Lecture 12: .NET Master of Information System Management

  2. Overview Basics Master of Information System Management

  3. .NET • Runtime environment called the Common Language Runtime (CLR) • Class library called the Framework Class Library (FCL) Master of Information System Management

  4. Common Language Runtime • Modern runtime environment • .NET compilers do not target a specific processor • The CLR must be present on the target machine • Safely manages code execution • JIT compilation from MSIL to machine executable • Security and Permission management Master of Information System Management

  5. Framework Class Library • Object oriented • Collections, console, network and file I/O • Database and XML support • Rich server side event model • Rich client side support for GUI construction • Support for building SOAP based web services • More than 3,500 classes Master of Information System Management

  6. C# Overview • C# is type safe (hard to access objects in inappropriate ways) • Automatic memory management • Exception handling • Array bounds checking • Support for checked arithmetic Master of Information System Management

  7. Hello World 1 Full Namespaces // Hello World 1 in C# class MyApp { public static void Main() { System.String x = "World"; System.Console.WriteLine("Hello " + x); } } MSCorLib.dll is one among many assemblies we can include. The Basic Class Library is spread over a couple of assemblies. The .exe file has bootstrap code to run the .NET run time. MyApp is in the global namespace Compile with csc -t:exe -out:HelloUser.exe -r:MSCorLib.dll HelloUser.cs Execute MSIL Managed Code with HelloUser The code runs within the Common Language Runtime (CLR) Master of Information System Management

  8. Hello World 2 Using System // Hello World 2 in C# using System; class MyApp { public static void Main() { String x = "World 2"; Console.WriteLine("Hello " + x); } } Compile With csc HelloUser.cs Execute The MS Intermediate Language .exe file with HelloUser Master of Information System Management

  9. Hello World 3 Without System // Hello World 3 in C# class MyApp { public static void Main() { String x = "World 3"; Console.WriteLine("Hello " + x); } } HelloUser.cs(9,7): error CS0246: The type or namespace name 'String' could not be found (are you missing a using directive or an assembly reference?) And more errors… Master of Information System Management

  10. Type System Unification 0 • The object class is the ultimate base class for both reference types and value types • Simple types in C# alias structs found in System • So, Simple types have methods int i = 3; string s = i.ToString(); • But follow the same semantics as simple types of old, e.g., i = 3; j = i; j = 2; // i still 3 Master of Information System Management

  11. Type System Unification 1 class MyMathApp { public static void Main() { float x = 2.3F; // Semantics are the same System.Single y = 1.0F; // Only the syntax differs float z = x + y; System.Console.WriteLine("Result = " + z); } } Master of Information System Management

  12. Type System Unification 2 // All types derive from System.Object (which corresponds to the primitive object // type.) class MyMathApp { public static void Main() { float x = 2.3F; // 2.3 is a double so use 'F' for float System.Single y = 1.0F; float z = x + y; object o = z; // object is part of C#, Object is in System System.Console.WriteLine("Object Result = " + o); } } Object Result = 3.3 Master of Information System Management

  13. Parameters1 Pass By Value // C# Parameters using System; public class Parameter1 { static void inc(int x) { ++x; } public static void Main() { int a = 30; inc(a); Console.WriteLine(a); // 30 is displayed } } Master of Information System Management

  14. Parameters 2 Pass by Reference // C# Parameters 2 using System; public class Parameter2 { static void inc(ref int x) { ++x; } public static void Main() { int a = 30; inc(ref a); Console.WriteLine(a); // 31 is displayed } } Master of Information System Management

  15. Parameters 3 Passing Objects // C# Parameters using System; public class Student { public int age; } public class Parameter3 { static void swap(Student x, Student y) { Student t = x; x = y; y = t; } Master of Information System Management

  16. public static void Main() { Student a = new Student(); a.age = 34; Student b = new Student(); b.age = 65; swap(a,b); Console.WriteLine(a.age +" " +b.age); // 34 65 } } Master of Information System Management

  17. Parameter 4 Passing Objects // C# Parameters using System; public class Student { public int age; } public class Parameter4 { static void swap(ref Student x, ref Student y) { Student t = x; x = y; y = t; } Master of Information System Management

  18. public static void Main() { Student a = new Student(); a.age = 34; Student b = new Student(); b.age = 65; swap(ref a,ref b); Console.WriteLine(a.age +" " +b.age); // 65 34 } } Master of Information System Management

  19. Parameters 5 Out Parameters // C# Parameters using System; public class Student { public int age; } public class Parameter5 { static void MakeAStudent(out Student x) { x = new Student(); // assignment is required } Master of Information System Management

  20. public static void Main() { Student a; MakeAStudent(out a); a.age = 34; Console.WriteLine(a.age); // 34 is displayed } } Master of Information System Management

  21. Parameter 6 Passing Arrays // C# Parameters using System; public class Parameter6 { static decimal Multiply(params decimal[] a) { decimal amt = 0.0m; foreach(decimal i in a) amt += i; return amt; } Master of Information System Management

  22. public static void Main() { decimal[] x = { 2.0m, 3.0m, 1.0m }; Console.WriteLine(Multiply(x)); // 6.0 is displayed } } Master of Information System Management

  23. Classes 1 // Classes may have members with protection levels // The default is private. class Student { public string name; int age; public Student(string n, int a) { name = n; age = a; } } class MyClassApp { public static void Main() { Student s = new Student("Mike",23); System.Console.WriteLine("Student " + s.name); // illegal to try to display the age from here } } Master of Information System Management

  24. Classes 2 internal class Student { public string name; int age; public Student(string n, int a) { // Classes default to 'internal' visibility. name = n; // MyClassApp is public and therefore age = a; // visible to external // assemblies. } } public class MyClassApp { public static void Main() { Student s = new Student("Mike",23); System.Console.WriteLine("Student " + s); } } Student Student Master of Information System Management

  25. Classes 3 Properties using System; class Student { private string name; private int age; public String StudentName { set { name = value; } get { return name; } } Master of Information System Management

  26. public int StudentAge { set { age = value; } get { return age; } } } public class MyClassApp { public static void Main() { Student s = new Student(); s.StudentName = "Mike"; // calls set s.StudentAge = 23; // calls set // call get Console.WriteLine(s.StudentName + ":" + s.StudentAge); } } Mike:23 Master of Information System Management

  27. Classes 4 Inheritance // C# Classes and Inheritance using System; class Student { private string name; private int age; public String StudentName { set { name = value; } get { return name; } } Master of Information System Management

  28. public int StudentAge { set { age = value; } get { return age; } } } Master of Information System Management

  29. class GradStudent : Student { private String underGraduateDegree; public String Degree { set { underGraduateDegree = value; } get { return underGraduateDegree; } } } Master of Information System Management

  30. public class DemoInheritance { public static void Main() { GradStudent s = new GradStudent(); s.StudentName = "Mike"; s.StudentAge = 23; s.Degree = "Philosophy"; Console.WriteLine(s.StudentName + ":" + s.StudentAge + ":" + s.Degree); } } Mike:23:Philosophy Master of Information System Management

  31. Classes 5 Polymorphism // C# Classes and Polymorphism using System; public class Student { private string name; private int age; public String StudentName { set { name = value; } get { return name; } } Master of Information System Management

  32. public int StudentAge { set { age = value; } get { return age; } } } Master of Information System Management

  33. public class GradStudent : Student { private String underGraduateDegree; public String Degree { set { underGraduateDegree = value; } get { return underGraduateDegree; } } } Master of Information System Management

  34. public class DoctoralStudent : GradStudent { private String thesisTitle; public String ThesisTitle { get { return thesisTitle; } } public DoctoralStudent(string thesis) { thesisTitle = thesis; } } Master of Information System Management

  35. public class DemoInheritance { public static void Main() { GradStudent s = new GradStudent(); DoctoralStudent d = new DoctoralStudent("The Semantic Web"); s.StudentName = "Mike"; s.StudentAge = 23; d.StudentName = "Sue"; d.StudentAge = 25; Console.WriteLine(s.StudentName + ":" + s.StudentAge); Console.WriteLine(d.StudentName + ":" + d.StudentAge); Display(s); Display(d); } public static void Display(Student x) { // Method takes any Student Console.WriteLine(x.StudentName + ":" + x.StudentAge); } } Master of Information System Management

  36. Type Constructors 1 // Classes may have "Type Constructors" internal class Student { public static int numberOfStudentsCreated; static Student() { // must take no args numberOfStudentsCreated = 0; } public string name; int age; public Student(string n, int a) { name = n; age = a; numberOfStudentsCreated++; } } Master of Information System Management

  37. public class MyClassApp { public static void Main() { Student s = new Student("Mike",23); Student t = new Student("Sue",23); System.Console.WriteLine("Student's created = " + Student.numberOfStudentsCreated); } } HelloUser Student's created = 2 Master of Information System Management

  38. GUI Programming (1) using System; using System.Windows.Forms; public class WindowGreeting { private String m_userName; public String UserName { set { m_userName = value; } get { return m_userName; } } Master of Information System Management

  39. public void Greet() { MessageBox.Show("Hello " + m_userName); } public static void Main(String[] a) { WindowGreeting wg = new WindowGreeting(); wg.UserName = "Mike"; wg.Greet(); } } Master of Information System Management

  40. Master of Information System Management

  41. GUI Programming (2) csc -r:System.Windows.Forms.dll NewOne.cs NewOne The escape key works too. Master of Information System Management

  42. GUI Programming (2) using System; using System.Drawing; using System.Windows.Forms; // Inherit from Form to control window public class WindowGreeting : Form { private String m_userName; private Button m_btnClose; private Label m_label; public WindowGreeting() { Console.WriteLine("constructing"); m_label = new Label(); m_label.Location = new Point(16,16); m_label.Size = new Size(136,24); m_label.Text = ""; Master of Information System Management

  43. m_btnClose = new Button(); m_btnClose.Location = new Point(48,50); m_btnClose.Size = new Size(56,24); m_btnClose.Text = "Discard"; m_btnClose.Click += new EventHandler(CloseButton_Click); this.Controls.Add(m_label); this.Controls.Add(m_btnClose); this.ClientSize = new Size(150, 90); this.CancelButton = m_btnClose; } Master of Information System Management

  44. void CloseButton_Click(Object sender, EventArgs e) { this.Close(); } public String UserName { set { m_userName = value; } get { return m_userName; } } public static void Main(String[] a) { WindowGreeting wg = new WindowGreeting(); wg.ShowDialog(); } } Master of Information System Management

  45. Networking 1 Visit a Web Site // Snarf.cs from C# in a Nutshell // Snarf.exe http://www.oreilly.com/catalog/csharpnut using System; using System.IO; using System.Net; using System.Text; class Snarf { static void Main(String[] args) { // args[0] holds a URL from the command line WebRequest req = WebRequest.Create(args[0]); WebResponse resp = req.GetResponse(); Master of Information System Management

  46. // read the data from the URL Stream s = resp.GetResponseStream(); StreamReader sr = new StreamReader(s,Encoding.ASCII); String doc = sr.ReadToEnd(); Console.WriteLine(doc); } } Suppose IIS is running with a virtual directory called MyNewWebApps Snarf http://localhost/MyNewWebApps/Index.html Displays the HTML code on the DOS Screen Master of Information System Management

  47. Distributed Objects using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting; using System; class MyClient { public static void Main() { ChannelServices.RegisterChannel(new TcpClientChannel()); RemoteStudent r = (RemoteStudent) Activator.GetObject( typeof(RemoteStudent), "tcp://localhost:6502/somestudent"); This client assumes the server is running. Master of Information System Management

  48. String name = r.getName(); int age = r.getAge(); Console.WriteLine("Student Name: " + name + " Age: " + age); } } Directory before compilation MyClient.cs Student.dll Student.cs Server.cs Compile with csc -t:exe -r:Student.dll MyClient.cs Run with MyClient Student Name: Mike Age: 23 Master of Information System Management

  49. The Remote Object // A Remote Student Object saved in Student.cs using System; public class RemoteStudent : MarshalByRefObject { private int age = 23; private String name = "Mike"; public int getAge() { return age; } public String getName() { return name; } } Compile with csc -t:library Student.cs Produces a DLL file Student.dll Master of Information System Management

  50. Publish the Object with Server.cs using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting; using System; class MyApp { public static void Main() { ChannelServices.RegisterChannel( new TcpServerChannel(6502)); RemotingConfiguration.RegisterWellKnownServiceType( Type.GetType("RemoteStudent, Student"), "SomeStudent", WellKnownObjectMode.SingleCall); Master of Information System Management

More Related