当前位置: 移动技术网 > IT编程>开发语言>c# > C# 6.0 内插字符串(Interpolated Strings )的使用方法

C# 6.0 内插字符串(Interpolated Strings )的使用方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

看interpolated strings之前,让我们先看ef core 2.0 的一个新的特性:string interpolation in fromsql and

executesqlcommand。

var city = "london";

using (var context = createcontext())
{
 context.customers
  .fromsql($@"
   select *
   from customers
   where city = {city}")
  .toarray();
}

sql语句以参数化的方式执行,所以是防字符串注入的。

@p0='london' (size = 4000)

select *
from customers
where city = @p0

一直认为interpolated strings只是string.format的语法糖,传给fromsql的方法只是一个普通的字符串,已经移除了花括号,并把变量替换成了对应的值。fromsql获取不到变量信息,怎么实现参数化查询的呢? ok,让我们从头看起吧。

什么是内插字符串 (interpolated strings)

内插字符串是c# 6.0 引入的新的语法,它允许在字符串中插入表达式。

var name = "world";
console.writeline($"hello {name}");

这种方式相对与之前的string.format或者string.concat更容易书写,可读性更高。就这点,已经可以令大多数人满意了。事实上,它不仅仅是一个简单的字符串。

内插字符串 (interpolated strings) 是什么?

用代码来回答这个问题:

var name = "world";
string str1 = $"hello {name}"; //等于 var str1 = $"hello {name}";
iformattable str2 = $"hello {name}";
formattablestring str3 = $"hello {name}";

可以看出,interpolated strings 可以隐式转换为3种形式。实际上式编译器默默的为我们做了转换:

var name = "world";
string str1 = string.format("hello {0}",name); //等于 var str1 = $"hello {name}";
iformattable str2 = formattablestringfactory.create("hello {0}",name);
formattablestring str3 = formattablestringfactory.create("hello {0}",name);
  • iformattable 从.net framwork 1 时代就有了,只有一个tostring方法,可以传入iformatprovider来控制字符串的格式化。今天的主角不是他。
  • formattablestring 伴随interpolated strings引入的新类。

formattablestring 是什么?

先看一段代码

var name = "world";
formattablestring fmtstring = $"hello {name}";
console.writeline(fmtstring.argumentcount); //1
console.writeline(fmtstring.format); //hello {0}
foreach (var arg in fmtstring.getarguments())
{
 console.writeline(arg); //world
 console.writeline(arg.gettype()); //system.string
}

可以看出formattablestring保存了interpolated strings的所有信息,所以ef core 2.0能够以参数化的方式来执行sql了。

ef core 中的注意事项

因为隐式转换的原因,在使用ef corefromsql 方法和 executesqlcommand方法时,需要特别小心。一不留神就会调入陷阱。

var city = "london";

using (var context = createcontext())
{
 //方法一,非参数化
 var sql = $" select * from customers where city = {city}";
 context.customers.fromsql(sql).toarray();

 //方法二,参数化
 context.customers.fromsql($" select * from customers where city = {city}").toarray();

 //方法三,参数化
 formattablestring fsql = $" select * from customers where city = {city}";
 context.customers.fromsql(fsql).toarray();

 //方法四,非参数化
 var sql = " select * from customers where city = @p0";
 context.customers.fromsql(sql, city).toarray();

}

第一种方法,因为sql的赋值被编译成string.format方法的调用,返回的是字符串。sql变量传入fromsql方法时,又经过一次system.string microsoft.entityframeworkcore.rawsqlstring隐式转换。但sql变量本身已经丢失了参数信息,所以无法实现参数化的查询。

第四种方法, 也是interpolated strings -> string -> rawsqlstring的转换过程,但因为变量是分开传入fromsql方法的,所以是以参数化的方式执行的。

其他

熟悉es2015的同学可以看看javascript中的实现,tagged template literals,这和interpolated strings 非常类似。

昨晚凌晨12点发帖,不知道为什么被移除首页了。感觉是篇幅不够的原因,重新加了点ef core注意事项,但超过1小时没办法重新回首页了。七年来的第一篇文章,有点遗憾。希望大家喜欢。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网