For some time we're using some of the new c# 7.0 features in our razor files. We've integrated the Roslyn compiler into our web projects, which are currently targetting .NET Framework 4.6.2.
Today I wanted to try out the tuples in a razor file, like so:
@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
}
This produces the following runtime exception:
CS8137: Cannot define a class or member that utilizes tuples because the compiler required type 'System.Runtime.CompilerServices.TupleElementNamesAttribute' cannot be found. Are you missing a reference?
CS8179: Predefined type 'System.ValueTuple`2' is not defined or imported
This led me to https://stackoverflow.com/a/40826779/2772845 which mentions that I should add the System.ValueTuple package to the project. But I've already got that added.
These are the packages used in the config:
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.8" targetFramework="net462" />
<package id="Microsoft.Net.Compilers" version="2.4.0" targetFramework="net462" developmentDependency="true" />
<package id="System.ValueTuple" version="4.4.0" targetFramework="net462" />
So, I'm back to using the old Tuple<>
right now.
I'd like to know if anyone knows what I'm overlooking.
Edit 11/16/2017:
So I've altered the public (string labelName, string sfName) GetNames(PurchaseType purchaseType)
into public ValueTuple<string, string> GetNames(PurchaseType purchaseType)
and that gives me the following exception:
CS0433: The type 'ValueTuple' exists in both 'System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
And that led me to 'ValueTuple<T1, T2>' exists in both 'System.ValueTuple ...' and 'mscorlib ...' giving the actual answer. I've got .NET Framework 4.7 installed, and since razor is compiled at runtime it just uses that version.
Kind regards, Rick