Bonsoir,

Sur l'utilisation d'un EventHandler, StyleCop me dit que je devrais utiliser les generics pour plus d'efficacité mais je ne trouve pas la syntaxe pour le faire.

Voilà mon code original:

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
 
public partial class Form2 : Form
{
    public event TopInstantEventHandler OnTopInstant;
 
    public Form2()
    {
        InitializeComponent();
    }
 
    private void button1_Click(object sender, EventArgs e)
    {
        if (OnTopInstant != null)
        {
            OnTopInstant(this, new GenererTopInstantEventArgs(DateTime.Now));
        }
    }
}
 
public class GenererTopInstantEventArgs : EventArgs
{
    public DateTime Instant { get; set; }
 
    public GenererTopInstantEventArgs(DateTime moment)
    {
        Instant = moment;
    }
}
 
public delegate void TopInstantEventHandler(object sender, GenererTopInstantEventArgs e);
Voilà le message de StyleCop:

Target : WindowsApplication1.TopInstantEventHandler (IntrospectionTargetType)
Resolution : "Remove 'TopInstantEventHandler' and replace its
usage with EventHandler<T>"
Help : http://msdn2.microsoft.com/library/ms182178(VS.90).aspx (String)
Category : Microsoft.Design (String)
CheckId : CA1003 (String)
RuleFile : Design Rules (String)
Info : "Do not declare new delegates to be used as event handlers
when targeting a version of the .NET Framework that
supports generics. Use an instance EventHandler<T>
instead."
Problème: si j'écris

Code : Sélectionner tout - Visualiser dans une fenêtre à part
public delegate void EventHandler<T>(object sender, GenererTopInstantEventArgs e);
Le compilo ne trouve plus le type de handler TopInstantEventHandler.

Quelqu'un aurti un petit exemple à me donner ? Ou une correction de mon code ?

Merci d'avance

Papy !