npocmaka npocmaka - 1 month ago 7
C# Question

Assign parameter value taken from a property in MSBuild

I'm trying to combine batch script and C# code using MSBuild and inline tasks.Looks like the only one way to pass a command line argument is to use a properties (

/property
command line switch in msbuild) and the only one way to access something external from the inline task is to use properties.

How can I combine the properties and parameters in MSBuild in way to make them accessible in the inline task?

Here's an example script (should be saves as
.bat
or
.cmd
) :

<!-- :
@echo off
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*msbuild.exe") do set "msb=%%#"

if not defined msb (
echo no .net framework installed
exit /b 10
)

rem :::::::::: calling msbuid :::::::::
call %msb% /nologo /noconsolelogger "%~dpsfnx0" /property:H=Hello
rem ::::::::::::::::::::::::::::::::::::
exit /b %errorlevel%
-->

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="A">
<A/>
</Target>
<UsingTask
TaskName="A"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >

<ParameterGroup >
<Z ParameterType="System.String">$(H)</Z>
</ParameterGroup>

<Task>
<Code Type="Fragment" Language="js">
<![CDATA[
System.Console.WriteLine("--"+Z+"--");
]]>
</Code>
</Task>
</UsingTask>
</Project>


as
/property:H=Hello
is passed to the , the intention is this script to print
--Hello--
but it only prints
----

Answer

If in the command line you use

 /property:"H='Hello'"

you can simply use

System.Console.WriteLine("--"+$(H)+"--");  

without the need for the <ParameterGroup /> block

Comments