1 / 66

.NET Framework, C# et .NET Remoting

.NET Framework, C# et .NET Remoting. Sébastien Boulay sebastien.boulay@the-gnou-community.net Master GI – option SRR 25 novembre 2004. Sommaire. .NET Framework Introduction Architecture et fonctionnement Standardisation et interopérabilité C# Concepts de base Types et variables

ojal
Download Presentation

.NET Framework, C# et .NET Remoting

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. .NET Framework, C# et .NET Remoting Sébastien Boulay sebastien.boulay@the-gnou-community.net Master GI – option SRR 25 novembre 2004

  2. Sommaire • .NET Framework • Introduction • Architecture et fonctionnement • Standardisation et interopérabilité • C# • Concepts de base • Types et variables • Classes • Fonctionnalités avancées • .NET Remoting • Principe • Configuration Demos .NET Framework, C# et .NET Remoting 2

  3. Introduction • Technologie proposée par Microsoft • Plateforme de développement et d’exécution • Orientée environnements distribués Objectifs • Contexte d’exécution robuste et sécurisé • Déploiement et administration simplifié • Intégration de l’existant • Développement d’applications simplifié .NET Framework, C# et .NET Remoting .NET Framework 3

  4. Architecture Visual Basic C# J# Managed C++ JScript … CLS (Common Language Specification) Classes .NET Framework ASP.NET Windows Forms ADO.NET et XML CLR (Common Language Runtime) Système d’exploitation Free BSD Windows Red Hat Debian … Plateforme .NET, C# et .NET Remoting Plateforme .NET 4

  5. Microsoft Intermediate Language • Format intermédiaire • Indépendant de la machine • Converti en langage machine par le CLR • Code managé Assemblage (assembly) • Manifest • Meta données • Code MSIL • Ressources .NET Framework, C# et .NET Remoting .NET Framework 5

  6. Common Language Runtime Base Class Library Support Thread Support COM Marshaler Type Checker Exception Manager Security Engine Debug Engine IL to Native Compilers Code Manager Garbage Collector Class Loader Plateforme .NET, C# et .NET Remoting Plateforme .NET 6

  7. Type Value Type Reference Type Built-in Value Type User Defined Self-Describing Interface Pointer Built-in Reference Type Integer Type Enum Name Equivalent Function String Floating Point Type Delegate Managed Object Typed Reference Boxed Value Type Unmanaged Boxed Enum Structural Equivalent Array Common Language Specification CTS (Common Type Specification) • Système de types unique • Partagé par les compilateurs, outils et le CLR Ensemble de règles  interopérabilité des langages .NET Framework, C# et .NET Remoting .NET Framework 7

  8. Fonctionnement Code source Code MSIL Meta données Compilateur Compilation Code natif A l’installation ou A la première exécution Compilateur JIT Exécution Plateforme .NET, C# et .NET Remoting Plateforme .NET 8

  9. Common Language Infrastructure • Basé sur le CLR et la CLS • Défini comme un standard • ECMA-334, C# (C sharp) Language Specification. • ECMA-335, Common Language Infrastructure, approved by the ECMA General Assembly of 13th December 2001.  • ECMA TR/84, related to Standard ECMA-335 .NET Framework, C# et .NET Remoting .NET Framework 9

  10. Multi-langages • Microsoft • Visual Basic • C# • J# • Managed C++ • JScript • Autres • Ada  Pascal  Scheme • Perl  Fortran  Mercury • Python  SmallTalk  Oz • Cobol  Caml  Oberon • Eiffel  Haskell  … .NET Framework, C# et .NET Remoting .NET Framework 10

  11. Multi-plateformes Et d’autres projets en cours comme DotGNU Portable.NET .NET Framework, C# et .NET Remoting .NET Framework 11

  12. Sommaire • .NET Framework • Introduction • Architecture et fonctionnement • Standardisation et interopérabilité • C# • Concepts de base • Types et variables • Classes • Fonctionnalités avancées • .NET Remoting • Principe • Configuration Demos .NET Framework, C# et .NET Remoting 12

  13. Présentation • Orienté objet, jusqu’au bout, tout est objet • Similaire au C++ et au Java • Premier langage orienté composant parmi la famille C / C++ • Dédié à la plateforme .NET • Conforme aux spécifications (CLS-compliant) • Permet d’exploiter l’ensemble des fonctionnalités .NET Framework, C# et .NET Remoting C# 13

  14. Opérateurs • Unaires : + - ! ~ ++x --x (T)x • Arithmétiques : * / % + - • Décalage : << >> • Relationnels : < > <= >= == != • Logiques : & ^ | • Conditionnels : && || ?: • Assignation : = *= /= %= += -= <<= >>= &= ^= |= Condition ? Expression 1 : Expression 2 .NET Framework, C# et .NET Remoting C# 14

  15. Instructions • Blocs { ... } ; • Libellés x: instruction; • Selections if(x == 1) switch(i) { { if(y == 2) case 0: { case 1: F(); Choix(); } break; else { default: G(); ChoixAutre(); } break; } } .NET Framework, C# et .NET Remoting C# 15

  16. Instructions • Itérations while(x != 0) { ... } for(int i = 0; i < n; i++) { ... } do { ... } while(x == 0); foreach(object o in collection) { ... } • Sauts break; continue; goto libelle; return value; • Gestion des exceptions try throw new Exception(); { ... } catch(ArgumentException e) { ... } catch(Exception e) { ... } finally { ... } .NET Framework, C# et .NET Remoting C# 16

  17. Types • Valeurs • Types prédéfinis • Enumérations • Structures • Allocation dans la pile (stack) • Ne peut être null • Références • Tableaux • Délégués • Classes • Interfaces • Allocation dans le tas (heap) • Peut être null .NET Framework, C# et .NET Remoting C# 17

  18. Types prédéfinis .NET Framework, C# et .NET Remoting C# 18

  19. Enumerations enum Color : long Color color; { switch(color) Red, { Green, case Color.Red: Blue ... } break; case Color.Blue: ... break; case Color.Green: ... break; default: break; } .NET Framework, C# et .NET Remoting C# 19

  20. Structures struct Point { public int x,y; } struct Point { public int x,y; public Point(int x, int y) { this.x = x; this.y = y; } } .NET Framework, C# et .NET Remoting C# 20

  21. Tableaux • Une dimension int[] myArray = new int[] {1, 3, 5, 7, 9}; string[] weekDays = {"Sun","Sat","Mon","Tue","Wed","Thu","Fri"}; • Plusieurs dimensions int[,,] myArray = new int [4,2,3]; int[,] myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}}; • Jagged (tableau de tableaux) int[][,] myJaggedArray = new int [3][,] { new int[,] { {1,3}, {5,7} }, new int[,] { {0,2}, {4,6}, {8,10} }, new int[,] { {11,22}, {99,88}, {0,9} } }; .NET Framework, C# et .NET Remoting C# 21

  22. Classes • Nom • Attribut d’accessibilité • publicnon limité • protectedlimité à la classe et aux classes dérivées • privatelimité à la classe • internallimité à l’assemblage • protected internallimité à l’assemblage et aux classes dérivées • Attribut abstract (abstrait) • Attribut sealed (sellé) • Classe de base (héritage simple de classe) • Interfaces implémentées (héritage multiple d’interfaces) • Membres .NET Framework, C# et .NET Remoting C# 22

  23. Membres • Constantes • Champs • Méthodes • Constructeurs • Destructeurs • Types • Propriétés • Indexeurs • Opérateurs • Événements .NET Framework, C# et .NET Remoting C# 23

  24. Constantes class A { public const double X = 1.0; public const double Y = 2.0; public const double Z = 3.0; } class B { public const int X = C.Z + 1; public const int Y = 10; } class C { public const int Z = B.Y + 1; } .NET Framework, C# et .NET Remoting C# 24

  25. Champs public class Color { public static readonly Color Black = new Color(0, 0, 0); public static readonly Color White = new Color(255, 255, 255); private byte red, green, blue; public Color(byte r, byte g, byte b) { red = r; green = g; blue = b; } } • Attributs d’accessibilité • publicnon limité • protectedlimité à la classe et aux classes dérivées • privatelimité à la classe • internallimité à l’assemblage • Attributs • new champ cachant (classe dérivé) • staticchamp de classe • readonly champ en lecture seule (initialisé par le constructeur) • volatile champ volatile .NET Framework, C# et .NET Remoting C# 25

  26. Méthodes • Nom • Attribut d’accessibilité • public non limité • protected limité à la classe et aux classes dérivées • private limité à la classe • internal limité à l’assemblage • Attributs new, static, virtual (virtuel), abstract, sealed, override (surcharge), extern • Type de retour • Paramètres .NET Framework, C# et .NET Remoting C# 26

  27. Méthodes abstraites public abstract class Shape { public abstract void Paint(Graphics g, Rectangle r); } public class Ellipse: Shape { public override void Paint(Graphics g, Rectangle r) { g.DrawEllipse(r); } } public class Box: Shape { public override void Paint(Graphics g, Rectangle r) { g.DrawRect(r); } } .NET Framework, C# et .NET Remoting C# 27

  28. class Test { static void Main() { D d = new D(); A a = d; B b = d; C c = d; a.F(); b.F(); c.F(); d.F(); } } Méthodes virtuelles class A { public virtual void F() { Console.WriteLine("A.F"); } } class B: A { public override void F() { Console.WriteLine("B.F"); } } class C: B { new public virtual void F() { Console.WriteLine("C.F"); } } class D: C { public override void F() { Console.WriteLine("D.F"); } } B.F B.F D.F D.F .NET Framework, C# et .NET Remoting C# 28

  29. Constructeurs (instance) • Initialiser l’instance d’une classe • En cas d’absence de constructeur, un constructeur sans paramètre est implicitement créé class Point { public double x, y; public Point() { this.x = 0; this.y = 0; } public Point(double x, double y) { this.x = x; this.y = y; } } .NET Framework, C# et .NET Remoting C# 29

  30. Constructeur (classe) • Initialiser une classe • Ne peut être appelé explicitement • Appelé automatiquement • Pas d’attribut d’accessibilité, pas de paramètre class A { public static int X; static A() { X = B.Y + 1; } } class B { public static int Y = A.X + 1; static B() {} static void Main() { Console.WriteLine("X = {0}, Y = {1}", A.X, B.Y); } } .NET Framework, C# et .NET Remoting C# 30

  31. Destructeur • Appelé automatiquement lors du processus de garbage collection • Ne peut être appelé explicitement • Pas d’attribut d’accessibilité, pas de paramètre class A { public static int X; static A() { X = B.Y + 1; } } class B { public static int Y = A.X + 1; static B() {} static void Main() { Console.WriteLine("X = {0}, Y = {1}", A.X, B.Y); } } .NET Framework, C# et .NET Remoting C# 31

  32. Types • Déclaration d’un type à l’intérieur d’une classe public class List { // Private data structure private class Node { public object Data; public Node Next; public Node(object data, Node next) { this.Data = data; this.Next = next; } } private Node first = null; private Node last = null; // Public interface public void AddToFront(object o) {...} public void AddToBack(object o) {...} public object RemoveFromFront() {...} public object RemoveFromBack() {...} public int Count { get {...} } } .NET Framework, C# et .NET Remoting C# 32

  33. Propriétés • Accéder aux caractéristiques d’un objet ou d’une classe • longueur d’une chaîne de caractères • taille d’un tableau • titre d’une fenêtre • dimensions d’une image • … • Deux comportements • Lecture (get) • Écriture (set) • Mêmes attributs que les méthodes .NET Framework, C# et .NET Remoting C# 33

  34. Propriétés abstract class A { int y; public virtual int X { get { return 0; } } public virtual int Y { get { return y; } set { y = value; } } public abstract int Z { get; set; } } class B: A { int z; public override int X { get { return base.X + 1; } } public override int Y { set { base.Y = value < 0? 0: value; } } public override int Z { get { return z; } set { z = value; } } } .NET Framework, C# et .NET Remoting C# 34

  35. Indexeurs • Indexer une classe à la manière d’un tableau public class Stack { private Node GetNode(int index) { Node temp = first; while (index > 0 && temp != null) { temp = temp.Next; index--; } if (index < 0 || temp == null) throw new Exception("Index out of range."); return temp; } public object this[int index] { get { return GetNode(index).Value; } set { GetNode(index).Value = value; } } } .NET Framework, C# et .NET Remoting C# 35

  36. Opérateurs public struct Digit { byte value; public Digit(byte value) { if (value < 0 || value > 9) throw new ArgumentException(); this.value = value; } public Digit(int value): this((byte) value) {} public static implicit operator byte(Digit d) { return d.value; } public static explicit operator Digit(byte b) { return new Digit(b); } } .NET Framework, C# et .NET Remoting C# 36

  37. Opérateurs public struct Digit { byte value; public Digit(byte value) { if (value < 0 || value > 9) throw new ArgumentException(); this.value = value; } public Digit(int value): this((byte) value) {} public static Digit operator+(Digit a, Digit b) { return new Digit(a.value + b.value); } public static Digit operator-(Digit a, Digit b) { return new Digit(a.value - b.value); } } .NET Framework, C# et .NET Remoting C# 37

  38. Opérateurs public struct Digit { byte value; public Digit(byte value) { if (value < 0 || value > 9) throw new ArgumentException(); this.value = value; } public Digit(int value): this((byte) value) {} public static bool operator==(Digit a, Digit b) { return a.value == b.value; } public static bool operator!=(Digit a, Digit b) { return a.value != b.value; } public override bool Equals(object value) { if (value == null) return false; if (GetType() == value.GetType()) return this == (Digit)value; return false; } } .NET Framework, C# et .NET Remoting C# 38

  39. Délégués • Pointeurs de méthodes (vulgarisation) • Encapsule à la fois un objet et une méthode • Peut « pointer » vers plusieurs méthodes delegate void D(int x); class C { public static void M1(int i) {...} public static void M2(int i) {...} } class Test { static void Main() { D cd1 = new D(C.M1); // M1 D cd2 = new D(C.M2); // M2 D cd3 = cd1 + cd2; // M1 + M2 D cd4 = cd3 + cd1; // M1 + M2 + M1 D cd5 = cd4 + cd3; // M1 + M2 + M1 + M1 + M2 } } .NET Framework, C# et .NET Remoting C# 39

  40. Événements • Mécanisme de notification • Basé sur les délégués Consommateur d’événements Traitant d’événements Générateur d’événements Événement registration callback Consommateur d’événements Traitant d’événements .NET Framework, C# et .NET Remoting C# 40

  41. Événements public delegate void EventHandler(object sender, EventArgs e); public class Button: Control { public event EventHandler Click; protected void OnClick(EventArgs e) { if (Click != null) Click(this, e); } } public class Form { Button Button1 = new Button(); public Form() { Button1.Click += new EventHandler(Button1_Click); } ~Form() { Button1.Click -= new EventHandler(Button1_Click); } void Button1_Click(object sender, EventArgs e) { Console.WriteLine("Button1 was clicked!"); } } .NET Framework, C# et .NET Remoting C# 41

  42. Espaces de noms • Conteneur de classes ou d’espaces de nom • Organiser des classes hiérarchiquement • Structurer les assemblages class A {} // A namespace X // X { class B // X.B { class C {} // X.B.C } namespace Y // X.Y { class D {} // X.Y.D } } namespace X.Y // X.Y { class E {} // X.Y.E } .NET Framework, C# et .NET Remoting C# 42

  43. Interfaces • Nom • Attribut d’accessibilité • publicnon limité • protectedlimité à la classe et aux classes dérivées • privatelimité à la classe • internallimité à l’assemblage • Interfaces de base (héritage multiple d’interfaces) • Membres • Méthodes • Propriétés • Indexeurs • Événements .NET Framework, C# et .NET Remoting C# 43

  44. Fonctionnalités avancées • Section critique, exclusion mutuelle class Account { int balance; public Account(int initial) { balance = initial; } int Withdraw(int amount) { if (balance < 0) throw new Exception("Negative Balance"); lock (this) { if (balance >= amount) { balance = balance - amount; return amount; } else { return 0; } } } } .NET Framework, C# et .NET Remoting C# 44

  45. Fonctionnalités avancées • Contrôle de débordement lors d’opérations arithmétiques sur les entiers class OverFlowTest { static short x = 32767, y = 32767; public static int myMethodCheck() { int z = 0; try { z = checked((short)(x + y)); } catch (System.OverflowException e) { System.Console.WriteLine(e.ToString()); } return z; } public static int myMethodUncheck() { int z = unchecked((short)(x + y)); return z; } } .NET Framework, C# et .NET Remoting C# 45

  46. Fonctionnalités avancées • Boxing / Unboxing • Mécanisme de conversion entre un type valeur et un type référence • boxing : type valeur  type référence • unboxing : type référence  type valeur Sur la pile Sur la pile Sur le tas Sur le tas i i 123 123 int i = 123; object o = i; int i = 123; object o = i; int j = (int)o; int i = 123; int i = 123; o o (i boxed) (i boxed) 123 123 int int object o = i; object o = i; 123 123 j 123 int j = (box)o; .NET Framework, C# et .NET Remoting C# 46

  47. Fonctionnalités avancées • Code unsafe • Manipulation de bas niveau (pointeurs) • Autorise les conversions de types • Autorise les opérations arithmétiques sur les pointeurs • Semblable à du code C inline • Allocation sur la pile (stackalloc) class Test { unsafe void Method() { char* buf = stackalloc char[256]; for(char* p = buf; p < buf + 256; p++) *p = 0; ... } } .NET Framework, C# et .NET Remoting C# 47

  48. Fonctionnalités avancées • Code unsafe • Fixer l’adresse mémoire d’une variable à l’exécution (fixed) class Test { static int x; int y; unsafe static void F(int* p) { *p = 1; } static void Main() { Test t = new Test(); int[] a = new int[10]; unsafe { fixed (int* p = &x) F(p); fixed (int* p = &t.y) F(p); fixed (int* p = &a[0]) F(p); fixed (int* p = a) F(p); } } } .NET Framework, C# et .NET Remoting C# 48

  49. Sommaire • .NET Framework • Introduction • Architecture et fonctionnement • Standardisation et interopérabilité • C# • Concepts de base • Types et variables • Classes • Fonctionnalités avancées • .NET Remoting • Principe • Configuration Demos .NET Framework, C# et .NET Remoting 49

  50. Principe • Invoquer des méthodes sur objets distants • Deux rôles • Client • Serveur • Canal de communication (http, tcp) Canal Système distant Système distant Proxy Objet serveur Objet client .NET Framework, C# et .NET Remoting .NET Remoting 50

More Related