jeffkl / RoslynCodeTaskFactory

An MSBuild CodeTaskFactory that uses Roslyn compiler for cross platform compatibility
MIT License
26 stars 3 forks source link

Include Reference "System.Numerics" not working #14

Closed jmajnek closed 6 years ago

jmajnek commented 6 years ago

Hello,

I'm trying to use your CodeTaskFactory like this:

<UsingTask TaskName="DoNothing" TaskFactory="CodeTaskFactory" AssemblyFile="$(RoslynCodeTaskFactory)">
    <ParameterGroup>
      <InputString ParameterType="System.String" Required="true" />
      <OutputString ParameterType="System.String" Output="true" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.Numerics" />
      <Reference Include="System.Core" />
      <Using Namespace="System.Linq" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[        
          this.OutputString = BigInteger(System.Text.Encoding.ASCII.GetBytes("")).ToString().Sum(x => x - '0').ToString();
    ]]>
      </Code>
    </Task>
  </UsingTask>

 <Target Name="TestTarget" AfterTargets="Build">
    <DoNothing InputString="foo">
      <Output TaskParameter="OutputString" PropertyName="TestBla" />
    </DoNothing>
</Target>

Building the .netstandard2.0 project throws the following exceptions:

Using your nuget package in a .NET 4.7 project and '$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll' as assemblyFile, the inline-tasks works well. Changing to your AssemblyFile returns the same errors.

I'm not sure if this is a bug or something in my code is wrong. Could please help me?

BR Jakob

jeffkl commented 6 years ago

It looks like you have some issues with your code:

<UsingTask TaskName="DoNothing" TaskFactory="CodeTaskFactory" AssemblyFile="$(RoslynCodeTaskFactory)">
  <ParameterGroup>
    <InputString ParameterType="System.String" Required="true" />
    <OutputString ParameterType="System.String" Output="true" />
  </ParameterGroup>
  <Task>
    <Reference Include="System.Runtime.Numerics" />
    <Reference Include="System.Text.Encoding" />
    <Using Namespace="System.Linq" />
    <Using Namespace="System.Numerics" />
    <Using Namespace="System.Text" />
    <Code Type="Fragment" Language="cs">
      <![CDATA[        
        this.OutputString = new BigInteger(Encoding.ASCII.GetBytes("")).ToString().Sum(x => x - '0').ToString();
]]>
    </Code>
  </Task>
</UsingTask>
  1. Referencing System.Numerics instead of System.Runtime.Numerics
  2. You do not need to reference System.Core
  3. Missing a using of System.Numerics
  4. Missing a new keyword in front of BigInteger

With these changes, I was able to run the task but of course it just prints 0.

<Target Name="TestTarget" AfterTargets="Build">
  <DoNothing InputString="foo">
    <Output TaskParameter="OutputString" PropertyName="TestBla" />
  </DoNothing>

  <Message Text="TestBla: '$(TestBla)'" Importance="High" />
</Target>
1>------ Build started: Project: NETStandard.ClassLibrary, Configuration: Debug Any CPU ------
1>NETStandard.ClassLibrary -> D:\RoslynCodeTaskFactory\src\Samples\NETStandard.ClassLibrary\bin\Debug\netstandard2.0\NETStandard.ClassLibrary.dll
1>TestBla: '0'
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Time Elapsed 00:00:01.410