Using nameof function in VS 2010

huangapple go评论51阅读模式
英文:

Using nameof function in VS 2010

问题

这个问题的解决方案是升级你的开发环境,以便能够使用 nameof 函数。Visual Studio 2010 不支持 nameof 函数,所以你需要升级到一个支持该功能的更高版本的 Visual Studio。升级到 Visual Studio 2015 或更高版本将使你能够使用 nameof 函数来传递数据到 ComboBox。

英文:

The project I am developing was created in visual studio 2010. I would like to make a small addition to this project. I need to use nameof function to pass data to combobox. But, Visual studio 2010 doesn't include nameof function.

public partial class Form1 : Form
{
    ComboBox _comboBox;
    List<Instructor> _instructors;

    public Form1()
    {
        //InitializeComponent();

        var set = new HashSet<Instructor>(new InstructorComparer());
        var xml = XElement.Load("test.xml");
        foreach (var node in xml.Elements("Test").Elements("Instructor"))
        {
            var instructor = new Instructor
            {
                Num = (int)node.Attribute("Num"),
                Name = node.Element("Name").Value
            };
            set.Add(instructor);
        }

        _instructors = set.ToList();

        _comboBox.DisplayMember = nameof(Instructor.Num);
        _comboBox.DataSource = _instructors;
    }
}

What solutions do you have for this problem? thanks

答案1

得分: 2

只需写"Num",而不是nameof(Instructor.Num),因为这是nameof计算的结果。

英文:

Just write "Num" instead of nameof(Instructor.Num) as this is what nameof evaluates to.

答案2

得分: 1

nameof 功能是 C# 6 的一部分。Visual Studio 2010 默认使用 C# 版本 4。可以通过使用更新的 .NET Framework SDK 进行更改。

如果你需要在当前 .NET Framework 中使用 C# 6 语言功能,可以尝试使用 Microsoft.Net.Compilers NuGet 包。如果将它添加到你的项目中,将使用此包中的 C# 编译器,而不是默认的编译器。

这个包本身依赖于 MSBuild 和 .NET Framework 的版本,所以你需要使用这个包的旧版本。请查看 版本 1.3.2,我能够在 VS 2013 中使用它,而下一个版本 (2.0.0) 需要更新的 MSBuild,进而需要更新的 .NET Framework。

以下是 Microsoft.Net.Compilers 包的版本列表及详细信息:
https://github.com/dotnet/roslyn/blob/main/docs/wiki/NuGet-packages.md

根据以下描述,版本 1.3.2 应该是正确的版本:

版本 1.x 对应 C# 6.0 和 VB 14(Visual Studio 2015 及更新版本)。例如,1.3.2 对应 Visual Studio 2015 的最新更新(更新 3)。

英文:

The nameof feature is part of C# 6. Visual Studio 2010 by default uses C# version 4. It can be changed by using a newer .NET Framework SDK.

If you need to use C# 6 language features with the current .NET Framework you can try using Microsoft.Net.Compilers NuGet package. If you add it to your project(s) the C# compiler from this package will be used instead of the default one.

This package itself depends on the version of MSBuild and .NET Framework, so you'll have to use some old version of this package. Please check version 1.3.2, I was able to use it with VS 2013, and the next version (2.0.0) required a newer MSBuild which in turn required newer .NET Framework.

Here is the list of versions of Microsoft.Net.Compilers package with the detailed information:
https://github.com/dotnet/roslyn/blob/main/docs/wiki/NuGet-packages.md

According to the following description, version 1.3.2 should be the right one:
> Versions 1.x mean C# 6.0 and VB 14 (Visual Studio 2015 and updates). For instance, 1.3.2 corresponds to the most recent update (update 3) of Visual Studio 2015.

huangapple
  • 本文由 发表于 2023年2月6日 13:38:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定