MSBuild UsingTask Resolve References

前端 未结 2 1718
悲&欢浪女
悲&欢浪女 2021-01-01 12:27

I feel like I\'ve fixed this before, but I can\'t remember how.

I have a tasks file that looks like this (CustomTasks.tasks):



        
相关标签:
2条回答
  • 2021-01-01 12:33

    This is actually easy to fix. Put your custom build tasks and dependencies in a different folder. Then dependencies are loaded correctly.

    For example like so:

    <UsingTask AssemblyFile="..\BuildTools\CustomTasks.dll" TaskName="MyCustomTask"/>
    
    0 讨论(0)
  • 2021-01-01 12:40

    Got tired and frustrated and took a direct approach...I don't think this is the same way I solved the problem previously...but maybe this will help someone else. Other, more elegant solutions are more than welcome.

      <Target Name="BeforeBeforeBuild" BeforeTargets="BeforeBuild">
        <HandleAssemblyResolve SearchPath="$(ProjectDir)\..\MSBuild\" />
      </Target>
      <UsingTask TaskName="HandleAssemblyResolve" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
        <ParameterGroup>
          <SearchPath ParameterType="System.String" Required="true" />
        </ParameterGroup>
        <Task>
          <Using Namespace="System" />
          <Using Namespace="System.IO" />
          <Using Namespace="System.Reflection" />
          <Code Type="Fragment" Language="cs">
            <![CDATA[
    AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => 
    { 
      var assemblySearchPath = Path.Combine(SearchPath, e.Name.Split(',')[0]);
      if (File.Exists(assemblySearchPath)) return Assembly.LoadFrom(assemblySearchPath);
    
      return null;
    };
    ]]>
          </Code>
        </Task>
      </UsingTask>
    
    0 讨论(0)
提交回复
热议问题