Generating C# events and delegates

 

C# event generation

An event is a member that enables a class or object to provide notifications. An event is declared like a field except that the declaration is provided by an "event" keyword and the type must be a delegate type.


Within a class that declares an event member, the event behaves just like a field of a delegate type (provided the event is not abstract and does not declare accessors). The field stores a reference to a delegate that represents the event handlers that have been added to the event. If no event handles are present, the field is null.

 

UML model

C# generated

using System;

using System.Diagnostics;

 

 

namespace CsEvent

{

 

    public class OtherClass

    {

        public delegate void MyDelegate();

 

    }

 

}

 

 

 

 

To become a C# event, the signal stereotyped with "CsEvent" stereotype must be a part of a class. Once a signal is stereotyped as an event, you can (by double clicking on the element) manage new values such as visibility and delegate association. An event must refer to a model's delegate.


Delegates can be operations issued from C# delegate containers (classes stereotyped "CsDelegateContainer") or C# delegates (operations stereotyped "CsDelegate"). The related reference can be created through a drag and drop operation.
Figure 40 shows both the standard "Signal" dialog box and the dialog box for an event (a signal stereotyped "CsEvent").



Figure 40. The "Signal" dialog box and the "Event" dialog box

 

 

C# delegate container generation

C# delegate containers (classes stereotyped "CsDelegateContainer") are used as containers of C# delegate types (operations stereotyped "CsDelegate"). The generated delegates belong to the current parent element.

 

 

UML model

C# generation

 

using System;

using System.Diagnostics;

 

 

namespace MyPackage

{

    public delegate void OneDelegate();

 

    public delegate void TwoDelegate(char Parameter, int Parameter1);

 

    public delegate void ThreeDelegate(string Parameter);

 

}

 

 

 

Adding the "CsDelegate" stereotype to operations in a delegate container serves no purpose.

 

 

C# delegate generation

Delegates enable scenarios that some other languages have addressed with function pointers. However, unlike function pointers, delegates are object-oriented, type-safe, and secure. A delegate declaration defines a class that is derived from the "System.Delegate" class. A delegate instance encapsulates one or more methods, each of which is referred to as a callable entity . For instance methods, a callable entity consists of an instance and a method on that instance. For static methods, a callable entity consists of just a method. Given a delegate instance and an appropriate set of arguments, one can invoke all of that delegate instance's methods with that set of arguments.


Stereotyping an operation "CsDelegate" will enable delegate signature and skip generation of all code notes attached to the operation.

 

 

UML model

C# generated

using System;

using System.Diagnostics;

 

 

namespace MyPackage

{

 

    public class Class

    {

        public delegate void Operation();

 

    }

 

}