Bonjour,

Ma problématique est la suivante :
Je cherche à capturer des évènements produits par les BO (MyObject) contenus dans une liste de BO (List<MyObject>).
En faite, je me demande s'il est possible via une ligne de code du genre :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
ObjectsList<>.Changed += new ChangedEventHandler(ListChanged);
de capturer génériquement l'ensemble des évènements "Changed" produit par les éléments constituant la liste "ObjectsList".

Ci-dessous, je vous propose un bout de code exemple sur laquelle j'ai essayé d’implémenter la capture générique.

Merci par avance de votre aide.

Salutation,

Sylum

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Text;
 
namespace CollectionApplication
{
 
    // A delegate type for hooking up change notifications.
    public delegate void ChangedEventHandler(object sender, EventArgs e);
 
    class MyObject
    {
        // An event that clients can use to be notified whenever the elements is changed.
        public event ChangedEventHandler Changed;
 
        // Invoke the Changed event.
        protected virtual void OnChanged(EventArgs e)
        {
            if (Changed != null)
                Changed(this, e);
        }
 
        public MyObject(int myInt, string myString)
        {
            _myInt = myInt;
            _myString = myString;
            OnChanged(EventArgs.Empty);
        }
 
        private int _myInt;
        public int MyInt
        {
            get { return _myInt; }
            set { _myInt = value; OnChanged(EventArgs.Empty); }
        }
 
        private string _myString;
        public string MyString
        {
            get { return _myString; }
            set { _myString = value; OnChanged(EventArgs.Empty); ; }
        }
    }
 
    class MyCollection
    {
        public List<MyObject> ObjectsList = new List<MyObject>();
 
        public MyCollection()
        {
            // Capture générique ne fonctionne pas
            // ObjectsList<>.Changed += new ChangedEventHandler(ListChanged);
 
            ObjectsList.Add(new MyObject(1, "One"));
            ObjectsList.Add(new MyObject(2, "Two"));
            ObjectsList.Add(new MyObject(3, "Three"));
 
        }
 
        // This will be called whenever the list changes.
        private void ListChanged(object sender, EventArgs e)
        {
            Console.WriteLine("This is called when the event fires.");
        }
 
    }
 
}