You can add namespace imports to generated C# code by setting the AdditionalNamespaces property on a Package; this is for modelers who need generated code to reference types from other namespaces.
Add namespaces to generated code
Set AdditionalNamespaces on the Package that owns the generated code.
- In MDriven Designer, select the relevant Package.
- Open the Property Inspector.
- Set AdditionalNamespaces to a comma-separated list of namespaces.
- Generate the code and build the project.
- Open a generated file and verify that the required namespace imports are present.
For example, to make types from two namespaces available to generated code, set AdditionalNamespaces to:
System.Xml, MyCompany.Project.Shared
After code generation, the generated code for that Package includes the corresponding namespace imports, allowing the generated code to refer to types in those namespaces.
ViewModel code generation
A ViewModel can be generated as a C# class when its Codegen option is selected. For generated ViewModels, MDriven uses AdditionalNamespaces from the Package of the root ViewModel class.
This matters when a ViewModel contains expressions, types, or generated members that need types from another namespace. Set the property on the root ViewModel class's Package, not on a Package that only contains a nested ViewModel class.
For example, if the root ViewModel class is in the Package SalesUI, add the required namespaces to SalesUI.AdditionalNamespaces. Those namespaces are then used when MDriven generates the ViewModel C# code.
Use a comma-separated list
Enter each namespace as a name and separate names with commas.
| Goal | AdditionalNamespaces value |
|---|---|
| Add one namespace | MyCompany.Project.Shared
|
| Add multiple namespaces | System.Xml, MyCompany.Project.Shared
|
AdditionalNamespaces adds namespaces for generated code to use. It does not replace the Package's own generated-code namespace.
Regenerate after changing the property
Changing AdditionalNamespaces does not update files until you run code generation again. Follow the normal code generation workflow, then build the project so that missing or incorrect type references are reported by the compiler.
If code generation fails or generated files cannot be updated, see Documentation:Codegen failed.
