npocmaka
npocmaka

Reputation: 57252

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


        echo -^- FROM BATCH

        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=From C#"
        rem ::::::::::::::::::::::::::::::::::::
        exit /b %errorlevel%
--> 


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

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

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

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

Upvotes: 3

Views: 1411

Answers (1)

MC ND
MC ND

Reputation: 70923

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

Upvotes: 2

Related Questions