在.VS 2015 NET 4.5中使用字符串插值和nameof [英] Using string interpolation and nameof in .VS 2015 NET 4.5

查看:270
本文介绍了在.VS 2015 NET 4.5中使用字符串插值和nameof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中使用了 $ hello {person} nameof(arg1)之类的东西,但是在检查项目属性时,我的目标是.NET 4.5。

I'm using things like $"hello {person}" and nameof(arg1) in my code, but on checking the project properties I'm targeting .NET 4.5.

这可以吗?我以为这些东西是在4.6中引入的。

Is this okay? I thought these things were introduced in 4.6.

该项目可以在我的机器上构建并正常运行-但我担心在部署它时会出问题。

The project builds and runs okay on my machine - but I'm worried something will go wrong when I deploy it.

推荐答案

这是编译器功能,而不是框架功能。我们在Visual Studio 2015中的.NET 3.5项目中成功使用了这两种功能。

It's a compiler feature, not a framework feature. We successfully use both features with our .NET 3.5 projects in Visual Studio 2015.

简而言之,编译器会翻译 $ hello {person} String.Format( hello {0},人) nameof(arg1) arg1

In a nutshell, the compiler translates $"hello {person}" to String.Format("hello {0}", person) and nameof(arg1) to "arg1". It's just syntactic sugar.

运行时看到一个 String.Format 调用(或字符串文字 arg1,分别)并且不知道(也不在乎)原始源代码的外观。从.NET Framework的早期开始就支持 String.Format ,因此没有什么可以阻止您定位到该框架的早期版本。

The runtime sees a String.Format call (or a string literal "arg1", respectively) and does not know (nor care) what the original source code looked like. String.Format is supported since the early days of the .NET Framework, so there's nothing preventing you from targeting an earlier version of the framework.

这篇关于在.VS 2015 NET 4.5中使用字符串插值和nameof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆