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

查看:12
本文介绍了在 .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}", person)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",分别)并且不知道(也不关心)原始源代码是什么样的.String.Format 在 .NET Framework 的早期就受到支持,因此没有什么可以阻止您针对早期版本的框架.

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天全站免登陆