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
=$
2019-11-6-Roslyn-how-to-use-WriteLinesToFile-to-write-the-semicolons-to-file的更多相关文章
- EOJ Monthly 2019.11 E. 数学题(莫比乌斯反演+杜教筛+拉格朗日插值)
传送门 题意: 统计\(k\)元组个数\((a_1,a_2,\cdots,a_n),1\leq a_i\leq n\)使得\(gcd(a_1,a_2,\cdots,a_k,n)=1\). 定义\(f( ...
- [New!!!]欢迎大佬光临本蒟蒻的博客(2019.11.27更新)
更新于2019.12.22 本蒟蒻在博客园安家啦!!! 本蒟蒻的博客园主页 为更好管理博客,本蒟蒻从今天开始,正式转入博客园. 因为一些原因,我的CSDN博客将彻底不会使用!!!(带来不便,敬请谅解) ...
- 2019.11.9 csp-s 考前模拟
2019.11.9 csp-s 考前模拟 是自闭少女lz /lb(泪奔 T1 我可能(呸,一定是唯一一个把这个题写炸了的人 题外话: 我可能是一个面向数据编程选手 作为一个唯一一个写炸T1的人,成功通 ...
- [6644] 02 Apr 23:11:58.976 # Creating Server TCP listening socket *:6379: bind: No such file or directory
redis报错: [6644] 02 Apr 23:11:58.976 # Creating Server TCP listening socket *:6379: bind: No such fil ...
- ArchLinux 2019.11.01安装流程--安装基本系统
安装前的一些话 本文是参考官方文档ArchLinux的Installation guide(简体中文)加实际操作编写的. 有啥都好说,转载时请注明作者,这是基本素质,也是法律要求 安装是在虚拟机上进行 ...
- 6424. 【NOIP2019模拟2019.11.13】我的订书机之恋
题目描述 Description Input Output Sample Input 见下载 Sample Output 见下载 Data Constraint 题解 lj题卡线段树 求出每个右端点往 ...
- Spring5最新完整教程IDEA版【通俗易懂2019.11月】
1.Maven找包: spring-webmvc spring-jdbc 2.Spring的本质是控制反转,依靠依赖注入来实现.以一个servcie对象为例,即是service暴露注入接口(构造,se ...
- 2019/11/12 CSP模拟赛&&考前小总结
写在前面的总结 离联赛只有几天了,也马上就要回归文化课了. 有点舍不得,感觉自己的水平刚刚有点起色,却又要被抓回文化课教室了,真想在机房再赖几天啊. 像19/11/11那场的简单题,自己还是能敲出一些 ...
- pycharm 激活码 2019/11最新福利(2)
812LFWMRSH-eyJsaWNlbnNlSWQiOiI4MTJMRldNUlNIIiwibGljZW5zZWVOYW1lIjoi5q2j54mIIOaOiOadgyIsImFzc2lnbmVlT ...
- 2019.11.11 洛谷月赛t3
题目背景 由于Y校的老师非常毒瘤,要求\(zhouwc\)在\(csp\)考前最后\(3\)天参加期中考,\(zhouwc\)非常生气,决定消极考试,以涂完卡但全错为目标.现在\(retcarizy\ ...
随机推荐
- Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks【*组合数学】
A. Kyoya and Photobooks time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【Leetcode 堆、快速选择、Top-K问题 BFPRT】有序矩阵中第K小的元素(378)
题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, 5, 9], [ ...
- 【Leetcode 堆、快速选择、Top-K问题 BFPRT】数组中的第K个最大元素(215)
这道题很强大,引出了很多知识点 题目 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5 ...
- PHP生成唯一的促销/优惠/折扣码,由字母和数字组成。
首先我们先搞清楚什么是促销/优惠/折扣码?它有什么用作: 每一个电子商务网站,现在有一种或多种类型的优惠/折扣/优惠券系统,给大家分享一下如何在PHP生成唯一的促销/折扣码.主要是实现一个优惠码系统, ...
- 【巨人的步伐以及人类的进击】BSGS algorithm
原问题 求ax≡b(mod p)的最小正整数解. 解法 实际上是以空间换取时间的算法. 先用散列表把 ai (i∈[0,p√)) 都储存起来. 然后再从小到大枚举 j (j∈[0,p√)) ,在散列表 ...
- 如何mock https请求
最近在测试项目过程当中,遇到客户端mock https请求的场景,但是默认用charles抓取出来的https请求是乱码的,对于这类请求如何来mock,有以下2种方式: 1.这里有篇http://co ...
- yum方式安装MySQL【转】
在CentOS7中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 另外至2919年5月4号, 默认安装的my ...
- CSS3摆动动画效果
效果图:红包在左右摇晃 代码如下: @keyframes upAnimation { 0 % { transform: rotate(0 deg);transition - timing - func ...
- 云原生生态周报 Vol. 5 | etcd性能知多少
业界要闻 1 Azure Red Hat OpenShift已经GA.在刚刚结束的Red Hat Summit 2019上,Azure Red Hat OpenShift正式宣布GA,这是一个微软和红 ...
- kubernetes API 访问控制在阿里云容器服务(ACK)上的实践
提起K8s API的访问控制,很多同学应该都会想到RBAC,这是K8s用来做权限控制的方法,但是K8s对API的访问控制却不止于此,今天我们就来简单介绍下K8s的访问控制以及ACK如何利用这套方法提供 ...