• 2019-11-6-Roslyn-how-to-use-WriteLinesToFile-to-write-the-semicolons-to-file


    title author date CreateTime categories
    Roslyn how to use WriteLinesToFile to write the semicolons to file
    lindexi
    2019-11-06 19:13:42 +0800
    2018-11-1 16:9:1 +0800
    Roslyn MSBuild 编译器

    As you know, the WriteLinesToFile will separate the item by semicolons. How can we use WriteLinesToFile to write the semicolons to file?

    For example, we need to write the code below to file.

    Console.WriteLine("I am lindexi");

    As you can see, we can find some problems in the code.

    The first thing is the code include quotation mark that we can not write the code directly.

    We can use property item with "CDATA" that CDATA is defined as blocks of text that are not parsed by the parser.

        <PropertyGroup>
          <SomeThing>
           <![CDATA[
                    Console.WriteLine("I am lindexi");
           ]]>
          </SomeThing>
        </PropertyGroup>
        <WriteLinesToFile File="lindexi.cs" Lines="$(SomeThing)"></WriteLinesToFile>

    You can find that your content in the file misses the semicolons character.

    We can use %3b to replace the semicolons.

        <PropertyGroup>
          <SomeThing>
           <![CDATA[
                    Console.WriteLine("I am lindexi")%3b
           ]]>
          </SomeThing>
        </PropertyGroup>
        <WriteLinesToFile File="lindexi.cs" Lines="$(SomeThing)"></WriteLinesToFile>

    After you run the target, you can find a file named lindexi.cs added in your project.

     <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
    
      <Target Name="T2" BeforeTargets="BeforeBuild">
        <PropertyGroup>
          <SomeThing>
           <![CDATA[
                    Console.WriteLine("I am lindexi")%3b
           ]]>
          </SomeThing>
        </PropertyGroup>
        <WriteLinesToFile File="lindexi.cs" Lines="$(SomeThing)"></WriteLinesToFile>
      </Target>
    
     </Project>

    Please open the file and then you can find the content in the file include the semicolons character.

    We can set the property in the WriteLinesToFile task to Overwrite the file.

    You can copy the code below to your project file and then run your project. You will see the console output something interesting.

      <Target Name="T1" BeforeTargets="BeforeBuild">
        <PropertyGroup>
          <SomeThing>
            <![CDATA[
    using System%3b
    
    namespace CeseacooteeGowgu
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("I am lindexi")%3b
            }
        }
    }
           ]]>
          </SomeThing>
        </PropertyGroup>
        <WriteLinesToFile File="lindexi.cs" Lines="$(SomeThing)" Overwrite="true"></WriteLinesToFile>
        <ItemGroup>
          <Compile Remove="@(Compile)"></Compile>
          <Compile Include="lindexi.cs"></Compile>
        </ItemGroup>
      </Target>

    I remove all your code and replace the code to the content in SomeThing property. And then I add the code that named lindexi.cs to compile.

    Using MSBuild Escape

    We can find this way should change the code. We can not copy the code in other to write to file and we should replace all the semicolons to %3b that is hard to change all the code.

    We can use MSBuild Escape to escape the semicolons and you can see the code.

      <Target Name="T1" BeforeTargets="BeforeBuild">
        <PropertyGroup>
          <SomeThing>
            <![CDATA[
    using System;
    
    namespace CeseacooteeGowgu
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("I am lindexi");
            }
        }
    }
           ]]>
          </SomeThing>
        </PropertyGroup>
        <WriteLinesToFile File="lindexi.cs" Lines="$([MSBuild]::Escape($(SomeThing)))" Overwrite="true"></WriteLinesToFile>
        <ItemGroup>
          <Compile Remove="@(Compile)"></Compile>
          <Compile Include="lindexi.cs"></Compile>
        </ItemGroup>
      </Target>

    The [MSBuild]::Escape can use property function to escape strings.

    How to: Escape Special Characters in MSBuild - Visual Studio

    In the CDATA section that follows,

    • %40 = @
    • %25 = %
    • %3B = ;
    • %24 = $
  • 相关阅读:
    IM开发快速入门(一):什么是IM系统?
    IM群聊消息的已读未读功能在存储空间方面的实现思路探讨
    Android保活从入门到放弃:乖乖引导用户加白名单吧(附7大机型加白示例)
    IM开发干货分享:我是如何解决大量离线消息导致客户端卡顿的
    LetsTalk_Android中引导用户加入白名单图-2
    LetsTalk_Android中引导用户加入白名单图
    基于Go的马蜂窝旅游网分布式IM系统技术实践
    2020年了,Android后台保活还有戏吗?看我如何优雅的实现!
    微信团队分享:极致优化,iOS版微信编译速度3倍提升的实践总结
    史上最通俗,彻底搞懂字符乱码问题的本质
  • 原文地址:https://www.cnblogs.com/lindexi/p/12086436.html
Copyright © 2020-2023  润新知