Modeling type parameter constraints in UML models

You can model type parameter constraints for generic classifiers and methods, and generate C# code by using the UML-to-C# transformation.

Before you begin
The C# profile must be applied to the model and the target Microsoft® Visual Studio® solution must be imported and open in the workspace.
About this task
When you run the UML-to-C# transformation, you can apply type parameter constraints to generic methods and classifiers such as classes, interfaces, structs, and delegates in the UML model, transform type parameter constraints to C# code, and validate generic classifiers against those constraints. Similarly, when you run the C#-to-UML transformation, you can transform type parameter constraints from the C# code to the UML model and validate generic classifiers.
Note: The type parameter constraint for operations in model, assembly types, and code types does not support the model validation.

To transform type parameter constraints to the C# code by using the UML-to-C# transformation:

  1. Create a model that includes a UML class, a class template parameter, and a C# constraint stereotype, such as type constraints (class, interface and type parameter), constructor constraints (Boolean), or metadata constraints (enumeration), is applied to the template parameter.
  2. Type parameter constraints for the type parameter, such as T.
  3. Right-click a UML class, then add an operation and then add a class template parameter to that operation.
  4. Right-click the class template parameter of the operation; then add a C# constraint stereotype with a type parameter value, such as Z.
What to do next

After you run the UML-to-C# transformation without any errors, you can validate whether the type parameter constraints, that are attached to the class and the operation, are correctly transformed into the C# code.

To enable the model validation against type parameters constraints, go to Windows > Preferences > Model validation > Constraints, and then select Type Constraint. The validation process verifies the template binding relationship and logs errors or warnings against the violation of type parameter constraints that are defined by the Type_Constraint constraints.