InsertClass Merge Strategy
Use the InsertClass merge strategy to insert your template output into an existing class in the output file. It's similar to the InsertRegion merge strategy, but instead of looking for region markers, CodeSmith Generator parses the existing C# or Visual Basic file, finds the class by name and replaces it. Settings let you keep the class's attributes, merge using/Imports statements, and decide what happens when the class isn't there.
Settings
Use the alias InsertClass and an initialization string of Name=Value settings separated by semicolons:
| Setting | Type | Default | Description |
|---|---|---|---|
ClassName |
String | None (required) | Name of the class to replace. |
Language |
String | The template's TargetLanguage |
C# or VB. The only supported languages. |
PreserveClassAttributes |
Boolean | False |
Keep the attributes on the existing class. By default, the whole existing class is replaced, including its attributes. |
OnlyInsertMatchingClass |
Boolean | False |
Insert only the matching class from the template output, instead of the whole output. |
MergeImports |
Boolean | False |
Merge the using/Imports statements of the existing file and the template output. |
NotFoundAction |
Enum | None |
What to do when the existing file doesn't contain the class (see below). |
NotFoundParent |
String | None | With NotFoundAction=InsertInParent, the name of the region or class to insert into. |
NotFoundAction takes one of these values:
| Value | Result |
|---|---|
None |
Don't merge anything; leave the existing file as it is. |
InsertAtBottom |
Append the template output to the end of the existing file. |
InsertInParent |
Insert the template output at the bottom of the region or class named by NotFoundParent. |
If the output file doesn't exist yet or is empty, Generator writes the template output as it is. If the existing file can't be parsed, Generator leaves it unchanged and logs an error.
Example
This example uses the following initialization string:
ClassName=Pet;Language=C#;PreserveClassAttributes=True;OnlyInsertMatchingClass=True;MergeImports=True
The existing file:
using System;
using System.ComponentModel.DataAnnotations;
namespace Petshop
{
[ScaffoldTable(true)]
public class Pet
{
public int Age { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
The template output:
using System;
using System.Text;
namespace Petshop
{
public class Pet
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get { return String.Format("{0} {1}", FirstName, LastName); }
}
}
}
The merged result:
using System;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Petshop
{
[ScaffoldTable(true)]
public class Pet
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get { return String.Format("{0} {1}", FirstName, LastName); }
}
}
}
The using statements from both files are combined, the [ScaffoldTable(true)] attribute is kept, and the class body comes from the template, so the Age property is gone.