|
1 | 1 | ---
|
2 | 2 | title: WriteCodeFragment Task | Microsoft Docs
|
3 |
| -description: Learn how MSBuild uses the WriteCodeFragment task to generates a temporary code file from the specified generated code fragment. |
4 |
| -ms.date: 11/04/2016 |
| 3 | +description: Learn how MSBuild uses the WriteCodeFragment task to generate a temporary code file from the specified generated code fragment. |
| 4 | +ms.date: 07/26/2023 |
5 | 5 | ms.topic: reference
|
6 | 6 | dev_langs:
|
7 | 7 | - VB
|
@@ -29,14 +29,79 @@ Generates a temporary code file from the specified generated code fragment. Does
|
29 | 29 |
|
30 | 30 | |Parameter|Description|
|
31 | 31 | |---------------|-----------------|
|
32 |
| -|`AssemblyAttributes`|Optional <xref:Microsoft.Build.Framework.ITaskItem>`[]` parameter.<br /><br /> Description of the attributes to write. The item `Include` value is the full type name of the attribute, for example, "System.AssemblyVersionAttribute".<br /><br /> Each metadata is the name-value pair of a parameter, which must be of type `String`. Some attributes only allow positional constructor arguments. However, you can use such arguments in any attribute. To set positional constructor attributes, use metadata names that resemble "_Parameter1", "_Parameter2", and so on.<br /><br /> A parameter index cannot be skipped.| |
33 |
| -|`Language`|Required `String` parameter.<br /><br /> Specifies the language of the code to generate.<br /><br /> `Language` can be any language for which a CodeDom provider is available, for example, "C#" or "VisualBasic". The emitted file will have the default file name extension for that language.| |
| 32 | +|`AssemblyAttributes`|Optional <xref:Microsoft.Build.Framework.ITaskItem>`[]` parameter.<br /><br /> Description of the attributes to write. The item `Include` value is the full type name of the attribute, for example, `System.AssemblyVersionAttribute`.<br /><br /> Each metadata is the name-value pair of a parameter. Parameters are assumed to be of type `String` in MSBuild 17.6 and earlier, but in MSBuild 17.7 and later, you can also use types other than `String` that are part of `mscorlib`. For example, you can use `true` and `false` Boolean values, integers, enums, and floating point types. The type is automatically inferred from the syntax. For a type not in `mscorlib`, specify the type for the parameter by providing metadata values in the form `{parameter}_TypeName`.<br/><br/>Some attributes only allow positional constructor arguments. However, you can use such arguments in any attribute. To set positional constructor attributes, use metadata names that resemble `_Parameter1`, `_Parameter2`, and so on. A parameter index can't be skipped.<br/><br/>In MSBuild 17.7 or later, you can also specify metadata of the form `{parameter}_IsLiteral` to instruct the task to interpret the parameter value text as is, without surrounding with quotes (as is done in the default case for string values). | |
| 33 | +|`Language`|Required `String` parameter.<br /><br /> Specifies the language of the code to generate.<br /><br /> `Language` can be any language for which a CodeDom provider is available, for example, "C#" or "VisualBasic." The emitted file will have the default file name extension for that language.| |
34 | 34 | |`OutputDirectory`|Optional <xref:Microsoft.Build.Framework.ITaskItem> parameter.<br /><br /> Specifies the destination folder for the generated code, typically the intermediate folder.|
|
35 |
| -|`OutputFile`|Optional <xref:Microsoft.Build.Framework.ITaskItem> output parameter.<br /><br /> Specifies the path of the file that was generated. If this parameter is set by using a file name, the destination folder is prepended to the file name. If it is set by using a root, the destination folder is ignored.<br /><br /> If this parameter is not set, the output file name is the destination folder, an arbitrary file name, and the default file name extension for the specified language.| |
| 35 | +|`OutputFile`|Optional <xref:Microsoft.Build.Framework.ITaskItem> output parameter.<br /><br /> Specifies the path of the file that was generated. If this parameter is set by using a file name, the destination folder is prepended to the file name. If it's set by using a root, the destination folder is ignored.<br /><br /> If this parameter isn't set, the output file name is the destination folder, an arbitrary file name, and the default file name extension for the specified language.| |
36 | 36 |
|
37 | 37 | ## Remarks
|
38 | 38 |
|
39 |
| - In addition to having the parameters that are listed in the table, this task inherits parameters from the <xref:Microsoft.Build.Tasks.TaskExtension> class, which itself inherits from the <xref:Microsoft.Build.Utilities.Task> class. For a list of these additional parameters and their descriptions, see [TaskExtension base class](../msbuild/taskextension-base-class.md). |
| 39 | +In addition to having the parameters that are listed in the table, this task inherits parameters from the <xref:Microsoft.Build.Tasks.TaskExtension> class, which itself inherits from the <xref:Microsoft.Build.Utilities.Task> class. For a list of these additional parameters and their descriptions, see [TaskExtension base class](../msbuild/taskextension-base-class.md). |
| 40 | + |
| 41 | +In MSBuild 17.7 and later, this task was updated to support a greater variety of parameter types for assembly-level attributes. MSBuild 17.6 and earlier supported only `String` as a parameter type for assembly-level attributes. With MSBuild 17.7 and later, you can construct any .NET assembly attribute, not only those whose parameters were string types, as in earlier versions of MSBuild. |
| 42 | + |
| 43 | +For example, to define the assembly-level attribute `CLSCompliant(true)`, which uses a Boolean parameter, you can use the following syntax: |
| 44 | + |
| 45 | +```xml |
| 46 | +<ItemGroup> |
| 47 | + <AssemblyAttribute Include="System.CLSCompliantAttribute"> |
| 48 | + <_Parameter1>true</_Parameter1> |
| 49 | + </AssemblyAttribute> |
| 50 | +</ItemGroup> |
| 51 | +``` |
| 52 | + |
| 53 | +The code generated depends on the target language. For C#, it would be as follows: |
| 54 | + |
| 55 | +```csharp |
| 56 | +[assembly: System.CLSCompliantAttribute(true)] |
| 57 | +``` |
| 58 | + |
| 59 | +Types defined in `mscorlib` are automatically inferred, such as Boolean in the previous example. You can define metadata of the form `{parameter}_TypeName` to specify types that can't be inferred. |
| 60 | + |
| 61 | +```xml |
| 62 | +<ItemGroup> |
| 63 | + <AssemblyAttribute Include="Microsoft.Owin.OwinStartup"> |
| 64 | + <_Parameter1>Microsoft.Examples.Startup</_Parameter1> |
| 65 | + <_Parameter1_TypeName>System.Type</_Parameter1_TypeName> |
| 66 | + </AssemblyAttribute> |
| 67 | +</ItemGroup> |
| 68 | +``` |
| 69 | + |
| 70 | +The code generated in C# is as follows: |
| 71 | + |
| 72 | +```csharp |
| 73 | +[assembly: Microsoft.Owin.OwinStartup(typeof(Microsoft.Examples.Startup))] |
| 74 | +``` |
| 75 | + |
| 76 | +For more complicated parameter values, you can use the `{parameter}_IsLiteral`. |
| 77 | + |
| 78 | +```xml |
| 79 | +<ItemGroup> |
| 80 | + <AssemblyAttribute Include="NUnit.Framework.Parallelizable"> |
| 81 | + <_Parameter1>NUnit.Framework.ParallelScope.Fixtures</_Parameter1> |
| 82 | + <_Parameter1_IsLiteral>true</_Parameter1_IsLiteral> |
| 83 | + </AssemblyAttribute> |
| 84 | +</ItemGroup> |
| 85 | +``` |
| 86 | + |
| 87 | +The previous example produces the following assembly attribute in C#: |
| 88 | + |
| 89 | +```output |
| 90 | +[assembly: NUnit.Framework.Parallelizable(NUnit.Framework.ParallelScope.Fixtures)] |
| 91 | +``` |
| 92 | + |
| 93 | +You can use any syntax that would normally be allowed in an attribute declaration in the language of the project. For an array parameter, you can use code like the following: |
| 94 | + |
| 95 | +```xml |
| 96 | +<ItemGroup> |
| 97 | + <AssemblyAttribute Include="TestAttribute"> |
| 98 | + <_Parameter1>new int[] { 1, 3, 5 } /* odd numbers */</_Parameter1> |
| 99 | + <_Parameter1_IsLiteral>true</_Parameter1_IsLiteral> |
| 100 | + </AssemblyAttribute> |
| 101 | +</ItemGroup> |
| 102 | +``` |
| 103 | + |
| 104 | +When you use `IsLiteral`, the syntax is interpreted by the appropriate compiler, and is therefore language-specific. If you have situations where more than one language share the same MSBuild import files and/or project files, you might need to use conditional syntax to ensure the code compiles with the relevant project-specific language. |
40 | 105 |
|
41 | 106 | ## See also
|
42 | 107 |
|
|
0 commit comments