This content applies
to version 7.5.2 or later. You
can model C# 3.0 partial methods to generate code. Partial methods
in the code can also be generated in the model using a C#-to-UML transformation.
Before you begin
To use the stereotypes required in this procedure, you must
apply the C# transformation profile to the model.
About this task
To create partial methods in the model:
Procedure
- In the Project Explorer view or diagram editor, add three
classes. For example, add classes Class1, Class2, and Class3.
- In the diagram editor Palette, click the Create tab;
then click the Class drawer.
- Click Class and then click the
diagram. Repeat this step for Class2 and Class3.
- Click Dependency; then click Class2
and drag a relationship to Class1. Repeat this step to create a dependency
from Class3 to Class1.
- Apply the «CSharp Partial» stereotype to each dependency
relationship.
- Select a dependency relationship.
- Click the Stereotype tab.
- Click Apply Stereotypes, select CSharp
Partial, and then click OK.
- Repeat these substeps for the remaining dependency relationship.
- In each dependent class, add an operation with the same
name. For example, in Class2 and Class3, add an operation named APartialMethod.
- In the diagram editor, right-click a dependent class;
then click .
- Type a name for the operation; then press Enter.
For example, type APartialMethod.
- Repeat these substeps for the remaining dependent class.
- To the operation in Class2, apply the «CSharp Partial Declaration»
stereotype.
- To the operation in Class3, apply the «CSharp Partial Definition»
stereotype.
Results
After running a UML-to-C# transformation, a class with two
partial implementations is created. The partial part containing the
operation with the «CSharp Partial Declaration» stereotype has a method
declaration without the method body. The partial part containing the
operation with the «CSharp Partial Definition» stereotype has a method
and its body.
Example
The following image shows a sample diagram of C# partial
methods.
After a UML-to-C# transformation is run, the following code
is generated:
//Begin section for file PartOne.cs
//TODO: Add using directives that you want preserved
//End section for file PartOne.cs
// @generated
// @C#_transform [
// element-name=PartOne
// ]
public partial class SomeClass
{
// @generated
partial void APartialMethod();
}
//Begin section for file PartTwo.cs
//TODO: Add using directives that you want preserved
//End section for file PartTwo.cs
// @generated
// @C#_transform [
// element-name=PartTwo
// ]
public partial class SomeClass
{
// @generated
partial void APartialMethod()
{
//TODO: Auto-generated method stub
throw new System.NotImplementedException();
}
}
What to do next
After modeling the partial methods, you can configure and
run a UML-to-C# transformation to generate the C# code.