我安装了Visual Studio 15 Preview 3并尝试使用新的元组功能
static void Main(string[] args)
{
var x = DoSomething();
Console.WriteLine(x.x);
}
static (int x, int y) DoSomething()
{
return (1, 2);
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,我收到错误:
未定义或导入预定义类型'System.ValueTuple'2'
根据博客文章,这个功能默认情况下应该"打开".
我做错了什么?
我刚刚开始在Visual Studio 2017中使用我的旧解决方案.只需打开旧IDE中的解决方案即可无缝工作.c#应用程序项目现在默认为c#7.0编译器.这些项目的属性页面(编译/高级)可以轻松选择编译器的目标语言版本,默认为最新版本.
我找不到在asp.net web项目中启用c#7.0的方法.如果我写一个如下声明:
if (int.TryParse("1", out int myInt)) { ... }
Run Code Online (Sandbox Code Playgroud)
IDE警告我说我需要使用该语言的7+版本.
我对这个主题的研究表明,我应该定位web.config文件的system.codedom编译器区域中的特定c#版本,以便定位最新的Roslyn版本.
我现在拥有的是:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
Run Code Online (Sandbox Code Playgroud)
针对c#6的目标是什么.只要我已经使用nuget下载了最新的Roslyn,c#7的正确设置是什么?
更新 下面是Web项目可用的编译选项的屏幕截图(它是意大利语VS2017,但应该很容易理解).没有可能在那里选择目标c#版本.
有一段时间我们在剃刀文件中使用了一些新的c#7.0功能.我们已将Roslyn编译器集成到我们的Web项目中,这些项目目前正在针对.NET Framework 4.6.2.
今天我想在剃刀文件中试用元组,如下所示:
@functions
{
public (string labelName, string sfName) GetNames(PurchaseType purchaseType)
{
switch (purchaseType)
{
case PurchaseType.New:
return (labelName: Booklist.New, sfName: SpecflowIdentifiers.BooklistItem.CheckBoxNew);
case PurchaseType.Rental:
return (labelName: Booklist.Rent, sfName: SpecflowIdentifiers.BooklistItem.CheckBoxRental);
case PurchaseType.SecondHand:
return (labelName: Booklist.Secondhand, sfName: SpecflowIdentifiers.BooklistItem.CheckBoxSecondHand);
default:
throw new ArgumentOutOfRangeException(nameof(purchaseType), @"should not get here");
}
}
}
@helper RenderCheckbox(PurchaseType purchaseType, int index, decimal priceTo)
{
var names = GetNames(purchaseType);
var x = name.labelName;
// render something
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下运行时异常:
CS8137:无法定义使用元组的类或成员,因为无法找到编译器所需类型"System.Runtime.CompilerServices.TupleElementNamesAttribute".你错过了参考吗?
CS8179:未定义或导入预定义类型'System.ValueTuple`2'
这导致我/sf/answers/2857874561/提到我应该将System.ValueTuple包添加到项目中.但我已经添加了这个.
这些是配置中使用的包:
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" …Run Code Online (Sandbox Code Playgroud) 假设我有 2 张桌子,分别是学生和学校。在我的学生表中,我有一个链接到学校记录的 fkSchoolId。但是,如果我检索我的记录如下
public static List<Student> GetByType(string connString, int type)
{
using (mydb_DataContext db = new mydb_dbDataContext(connString))
{
return (from t1 in db.Students
where t1.type = type
select t1).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
我将拥有可以在 foreach 循环中访问的 Student 对象列表。但是当我做如下操作时,在检索学校名称时会出错。
foreach(Student student in DAL.StudentLogic.GetByType(5))
{
string schoolName = student.School.Name;
}
Run Code Online (Sandbox Code Playgroud)
System.ObjectDisposedException: '无法访问已处理的对象。对象名称:'DataContext 在 Dispose 后访问。'。
我可以知道如何获取存储在返回对象中的外部信息以便我可以访问它们吗?或者更好的方法,如果我可以指定只加载学校名称?
更新:如果我按照以下操作,它会起作用,但不确定它会对性能产生多大影响。下周我将做一个基准测试并再次更新这个主题。
public static List<Student> GetByType(string connString, int type)
{
using (mydb_DataContext db = new mydb_dbDataContext(connString))
{
List<Student> students = (from t1 in db.Students where t1.type = type …Run Code Online (Sandbox Code Playgroud)