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
Just write "Num" instead of nameof(Instructor.Num) as this is what nameof evaluates to.
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.
Related
I was using the "or pattern" in my C# code, but I am getting the following error:
CS8370 Feature 'or pattern' is not available in C# 7.3. Please use
language version 9.0 or greater.
I am targeting .NET Framework 4.7.2 and my .csproj file does not have a <LangVersion> element. However, when I add the following element to my .csproj file, the error disappears and my code compiles correctly:
<LangVersion>default</LangVersion>
I was expecting the default value for the <LangVersion> element to just use the standard framework c# language which I expect to be c# 7.3. So why does adding this element fix the error and allow me to use the "or pattern"? Am I involuntarily using a newer c# language version and have to expect unwanted issues?
I'm using Visual Studio 17.4.1.
Here is the sample code using the pattern:
static void Main()
{
var e = MyEnum.A;
var result = e is MyEnum.A or MyEnum.B; //shows CS8370 if used without <LangVersion>default</LangVersion>
}
public enum MyEnum
{
A,
B,
C
}
Microsoft states that the default is C# 7.3 for .net framework projects:
If you don't use <LangVersion>default</LangVersion>, Visual Studio compiler uses Frameworks C# language version default as you showed in your picture.
But if you use <LangVersion>default</LangVersion>, it uses the latest released major version of the compiler that is bundled with your Visual Studio installation.
You can see that if you scroll down more in the Microsoft page, it specifies what the "default" keyword means.
Additional note:
If you use default and VS2017, the "or pattern" might still not be supported due to the compiler shipping with VS2017 not being high enough version
Environment: vs 2022 v 17.4.0, with NET7 sdk installed
I have a multi target project net7.0;net472:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net7.0;net472</TargetFrameworks>
<LangVersion>11.0</LangVersion>
<!--others-->
</PropertyGroup>
I tried to use the new feature required in c# 11 as given below:
public class Person
{
public required int Id { get; set; }
}
I get a compilation error in net472:
Error CS0656 Missing compiler required member 'System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute..ctor'
Error CS0656 Missing compiler required member 'System.Runtime.CompilerServices.RequiredMemberAttribute..ctor'
My workaround solution is using conditional compilation as:
public class Person
{
public
#if NET7_0
required
#endif
int Id { get; set; }
}
What should I do to support the new features 'required' of c# 11 in a multi target project has net47x?
C# 11 language features is definitely not supported in Framework 4.7.2, from this table: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version it shows the default language version for NET Framework was C# 7.3, and this SO answer C# 8 was starting to have problems. C# 8 features in .NET Framework 4.7.2
As noted in the last link, some features may have been included, but was not officially supported.
If you absolutely have to multi-target, you either have to do your conditional compilation, or be mindful of what language features you use, limiting it to C# 7.3. You could try updating to NET Framework 4.8, but I think the most you will gain is support for C#8.
Just like it was the case with some new language features in older C# versions, this will work when you create the missing attributes yourself. For the required keyword you will need this attributes:
RequiredMemberAttribute
CompilerFeatureRequiredAttribute
IsExternalInit (for the init keyword in CompilerFeatureRequiredAttribute)
But since this is not officially supported, there might be problems either now or in the future.
You can use the PolySharp package to get these (and other) classes in your projects.
I want to execute Visual Studio command to import/export settings file in C#.
Can I do this using DTE2. If yes how to do?
How to initialize DTE2 and do..Please give the complete code.
Can I make use of this?
ExecuteCommand("Tools.ImportandExportSettings", "/export:\"C:/temp/setttings.vssettings\"")
If yes - then how to initialize dte2 and call the method?
You can try the following code to execute Visual Studio command to import/export settings file in c#.
First, you need to install nuget package ->EnvDTE.
Second, you can use the following console app to get the settings file.
class Program
{
static void Main(string[] args)
{
var filename = "D:\\own.vssettings";
var dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.
GetActiveObject("VisualStudio.DTE.16.0"); // version neutral
dte.ExecuteCommand("Tools.ImportandExportSettings", "/export:" + filename);
}
}
We need to note that if your vs version is vs 2010, we should use VisualStudio.DTE.10.0,
If your vs version is vs2013, we should use VisualStudio.DTE.12.0, If your vs version is
vs2017 or later, we should use VisualStudio.DTE.16.0.
My vs version is vs2017, so I used VisualStudio.DTE.16.0.
Result:(The part of setting file)
I have an issue with compilation of SSDT SQL Server Database Project by using Visual Studio 2015. I want to use C# 6 features inside my database project, but it seems like it is unsupported.
For example, I have added the next class in my db project:
namespace Database1
{
class ClassFile1
{
public string Str { get; } = string.Empty;
}
}
I have tried to compile this, but I got the error:
CS1519: Invalid token '=' in class, struct, or interface member declaration
I have found out that the reason of this error is wrong version of compiler that used by VS 2015. The next compilation line is generated by VS:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702,2008 /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /errorendlocation /preferreduilang:en-US /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\mscorlib.dll" /debug+ /debug:full /optimize- /out:obj\Debug\Database2.dll /subsystemversion:6.00 /target:library /warnaserror- /utf8output ClassFile1.cs
As you can see, C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe is used and it is wrong.
I have tried compile db project from Developer Command Prompt for VS 2015 and this was done successfully, because inside this prompt csc.exe is Roslyn compiler (C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe) that support C# 6 features. You can check question How to run Roslyn instead csc.exe from command line?
I tried to compile the project by using MSBuild 14.0 and it was successfully done too.
The question is: how can I change/override version of compiler that my VS used for compilation of SSDT DB project from old C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe to new Roslyn compiler (C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe)?
I was trying to do this as well and faced the same problem. My workaround is to create a separate regular class library project. I moved the C# code from my Database project into the new library (which will be compiled using C# 6/Roslyn by VS 2015). And then reference this class library from my Database project.
Just remember to set the properties of the reference to the class library: Model Aware=true and Generate Sql Script=true. I haven't tested this thoroughly, but I was able to deploy it from my Database project by using the publish command, and call the function inside the class library.
Good news!
Visual Studio 2017 will use the current C# compiler for SSDT Database Projects, so all the features of
C# 6 will finally work in SQL Server assemblies.
The path of the used compiler is:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\csc.exe
Because all of the C# 6 features are just pure compiler features you can now even use them if you are only targeting .NET 3.5 and SQL Server 2008.
In Visual Studio 2017 RC, I was also able to use some C# 7 features like expression bodied constructors, the new out variables, ref returns, the enhanced switch/is pattern matching and the improved binary/hex literals. But I was unable to use local functions or anything related to tuples like the new tuple return types, tuple literals or the new deconstruction declaration.
Maybe this behavior will change in the final release of Visual Studio 2017.
You will need to target the version of clr that is used by the version of SQL that you will be deploying to:
SQL 2005-2008 R2 = CLR 2
SQL 2012 = CLR 4
You can't just run any version of the clr that is on the machine I am afraid.
ed
Since switching from VS 2013 to VS 2015 and using some new C# 6 features, our daily builds in Visual Studio Online have begun failing.
The errors on the build are all pointing to the new auto property feature but I assume all new features will cause this.
An example piece of code that causes a failure is using:
public int MyFavouriteNumber { get; set; } = 7;
instead of
private int _myFavouriteNumber = 7;
public int MyFavouriteNumber
{
get { return _myFavouriteNumber; }
set { _myFavouriteNumber = value; }
}
I've had a look around my build configuration, but I can't see anything that relates to C# 6 or Roslyn.
What do I have to change to make my daily builds work again?
Edit
Here's an example error (they're all the same, for auto properties).
Models\Core\Bookings\BookingProduct.cs (29, 0)
Invalid token '=' in class, struct, or interface member declaration
Models\Core\Bookings\BookingProduct.cs (29, 0)
Invalid token '(' in class, struct, or interface member declaration
And here is the offending line:
public virtual IList<BookingPricingAddon> AddonsPricing { get; set; } = new List<BookingPricingAddon>();
TheLethalCoder's comments pointed me in the right direction.
The problem was that all of my projects were using Default as the target Language Version, which is fine if you're using VS 2015, however, my .sln file had the following opening 3 lines:
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
Apparently, Visual Studio Online uses this to work out which version of MSBuild to use. (It was using 12).
Upgrading my solution to the following:
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
allowed Visual Studio Online to see that my IDE was using Roslyn, and therefore used MSBuild 14.
The easiest way I found to upgrade is to click on the solution in Solution Explorer and then going to File > Save As > Solution File and overwrite your existing solution file, it just upgrades the first 3 lines.
My builds are now successful.
I think the same could be achieved by setting the Language Version on each of your projects. This can be done by going to your .csproj files and navigating to:
Properties > Build > Advanced > Language Version > C# 6.0
A good reference about the Default settings in each IDE can be found here.