Modeling C# partial methods

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

  1. In the Project Explorer view or diagram editor, add three classes. For example, add classes Class1, Class2, and Class3.
    1. In the diagram editor Palette, click the Create tab; then click the Class drawer.
    2. Click Class and then click the diagram. Repeat this step for Class2 and Class3.
  2. Click Dependency; then click Class2 and drag a relationship to Class1. Repeat this step to create a dependency from Class3 to Class1.
  3. Apply the «CSharp Partial» stereotype to each dependency relationship.
    1. Select a dependency relationship.
    2. Click the Stereotype tab.
    3. Click Apply Stereotypes, select CSharp Partial, and then click OK.
    4. Repeat these substeps for the remaining dependency relationship.
  4. In each dependent class, add an operation with the same name. For example, in Class2 and Class3, add an operation named APartialMethod.
    1. In the diagram editor, right-click a dependent class; then click Add UML > Operation.
    2. Type a name for the operation; then press Enter. For example, type APartialMethod.
    3. Repeat these substeps for the remaining dependent class.
  5. To the operation in Class2, apply the «CSharp Partial Declaration» stereotype.
  6. 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.
Diagram view of a partial method.

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.

Feedback