I don't understand why this will not compile when typed out in a .cshtml/Razor page:
#($"{"\""}") <-- this does not work
#($"{"'"}") <-- this also does not work
#($"{"a"}") <-- this DOES work
The error states:
RZ1000: Unterminated string literal. Strings that start with a quotation mark (") must be terminated before the end of the line. However, strings that start with # and a quotation mark (#") can span multiple lines.
CS1002: ; expected
CS1513: } expected
It appears that non-quote characters work fine, but a double-quote or single-quote character breaks Razor's ability to parse the interpolated string, even when the quote character is escaped.
All the examples work fine in a plain .cs file:
public sealed class Test
{
public string x = $"{"\""}";
public string y = $"{"'"}";
public string z = $"{"a"}";
}
What gives?
My environment is as follows:
.NET SDK: v7.0.102
.NET Runtime: v7.0.2
C#: v11 (preview)
Visual Studio Community 2022 (64-bit): v17.4.4
It's a bug, known by the .NET guys as far back as 2015 at least.
The dotnet engineers on the dotnet/aspnetcore github page felt the work required to fix it outweighed the benefits, however:
Jun 3, 2015
After investigating this issue further turns out the requirements to complete this are pretty high (need to understand C# close to entirety at the Tokenizer level). Will revisit this later.
2 months later...
Aug 14, 2015
Moving to backlog because we feel these cases are not common in MVC views, and have trivial workarounds (e.g. use string.Format() instead of string interpolation).
https://github.com/dotnet/aspnetcore/issues/4976
Related
I've googled this for a couple of hours and couldn't find anything.
So, I've got a certificate which has "ОАО" (russian letters) in its Simple Name for Subject.
I get this value using this code:
var x509 = new X509Certificate2(certificateBytes);
var subjectName = x509.GetNameInfo(X509NameType.SimpleName, forIssuer: false);
On Windows, this code returns 'ОАО' and this gets saved into MS SQL.
But on Linux in Docker container this code returns escaped unicode characters, but in caps.
Here they are:
\U041E\U0410\U041E
Instead of:
\u041E\u0410\u041E
And this caps is getting saved instead of 'ОАО'. So when I do nothing, it's \U1234 and it's written as-is in database: literally \U1234. But when I perform .ToLower()-transformation, this code transforms to \u1234 and is recognized as 'ОАО'.
So I've come up with a hack but I'm not sure it's the right thing to do. Anyway, that does save my text as 'ОАО' in database:
var subjectName = Regex.Unescape(x509.GetNameInfo(X509NameType.SimpleName, forIssuer: false).ToLowerInvariant());
What do you think? Is this a bug? Why does this method behave this way? Why it 'capses' unicode escape /u-character?
Is this a correct way to solve the problem?
Can I encounter a situation in which I'll find some unicode case-sensitive characters in which case I can lose the characters or they are case insensitive?
Is this a bug?
Yes it was a bug and has been fixed in .NET 6 RC2 (which is yet to be released, but is the next release of .NET 6 previews).
The Linux implementation of this with OpenSSL was inadvertently escaping non-ASCII text.
I am reading a long text file containing a sql query using StreamReader then using StringBuilder to create a string that gets run against a database. Once the string is created I checked the value and three dots ... appear within the string causing the query to fail when I run it against the database. Why is this happening? What can I do to keep it from happening?
string script;
if (File.Exists(path))
{
using(StreamReader sr = File.OpenText(path))
{
StringBuilder sb = new StringBuilder();
while(!sr.EndOfStream)
{
sb.Append(sr.ReadLine());
}
script = sb.ToString();
}
}
UPDATE: I should add that the three dots appear at character position 16384 every time. Not sure of the significance of this
UPDATE: It appears the string is being truncated at runtime. the file contiains 48080 characters but is being truncated in the middle at position 16384 making the string variable 32768.. Is this the max character count for a string?
I have a definite answer for you: Microsoft says that what you are experiencing is a bug in Visual Studio 2015. They have released an "Update 2" for Visual Studio 2015 that is reported to correct the issue.
I am trying to get this update installed by my admin, but in the meantime, a feasible workaround is to load the text in the JSON Visualizer instead. It will show an error that it is, of course, not valid JSON since it is SQL, but it will display the whole text of the string.
Download Update 2 from here:
https://www.visualstudio.com/en-us/news/vs2015-update2-vs.aspx
See the bug report here:
https://connect.microsoft.com/VisualStudio/feedback/details/2016177/text-visualizer-misses-corrupts-text-in-long-strings
I had the same problem while I was debugging the very long query string.
It turns out, Visual Studio(mine is 2015) Debugger will truncate long strings after certain amount of characters for ease of reading. So even though you are seeing three dots(...) in Text Visualizer, actual value doesn't have that three dots.
To my knowledge, visual studio 2012 debugger doesn't add three dots. I haven't found a way how to turn the feature off in VS2015, but you can use html visualizer or json visualizer as alternative solution.
I have a feeling that you are checking the values in your debugger, where the long query text is being partially shown and ends with an ellipsis (...)
Again guessing here, but seems like you join your lines of SQL into one single line, and if the lines in the file do not end with whitespace character, then the query will get messed up. Probably that is the reason why your SQL query does not work.
By the way, you can write the code you have far more succinctly as below
if (File.Exists(path))
script = string.Join(" ", File.ReadLines(path));
I am not sure if this is something that I am doing but I am seeing some weird issues with interpolated strings. Here is an example
Trace.WriteLine($"Raising event {e.EventName} for document {e.DocumentId}", "Info");
In above case e.EventName = DOCUMENT_CREATE and e.DocumentId = 111679
so the result should be like following
Raising event DOCUMENT_CREATE for document 111679
but it is printing like
Raising event DOCUMENT_CREATE� for document 111679
Same is the case in some other places where I am using string interpolation. What could be causing it?
Another piece of info, client have installed .net 4.6.1 RC. Is there a bug in .net 4.6.1 RC?
Well it was related to string interpolation but not entirely. We process our assemblies through SmartAssembly, and smart assembly messed up the interpolated strings by changing the encoding. SmartAssembly doesn't do that for non interpolated strings.
I have just been checking out the new string interpolation feature in C# 6.0 (refer to the Language Features page at Roslyn for further detail). With the current syntax (which is expected to change), you can do something like this (example taken from a blog post I'm writing just now):
var dob2 = "Customer \{customer.IdNo} was born on \{customer.DateOfBirth:yyyyMdd}";
However, I can't seem to include dashes in the formatting part, such as:
var dob2 = "Customer \{customer.IdNo} was born on \{customer.DateOfBirth:yyyy-M-dd}";
If I do that, I get the error:
Error CS1056 Unexpected character '-' StringInterpolation Program.cs 21
Is there any way I can get dashes to work in the formatting part? I know I can just use string.Format(), but I want to see if it can be done with string interpolation, just as an exercise.
Edit: since it seems like nobody knows what I'm talking about, see my blog post on the subject to see how it's supposed to work.
The final version is more user friendly:
var text = $"The time is {DateTime.Now:yyyy-MM-dd HH:mm:ss}";
With the version of string interpolation that's in VS 2015 Preview, you can use characters like dashes in the interpolation format by enclosing it in another pair of quotes:
var dob2 = "Customer \{customer.IdNo} was born on \{customer.DateOfBirth : "yyyy-M-dd"}";
Trying: T obj = JsonSerializer.DeserializeFromString<T>(jsonData);
on a string that has several \n's throughout it. JayRock's library successfully deserializes this like: T obj = (T)JsonConvert.Import(typeof(T), jsonData);
Is this a bug, or do I need to manually strip out newlines?
The problem I ran into wasn't the \n's, but instead was the lack of public properties on my DTO's.
RE: Can ServiceStack.Text deserialize JSON to a custom generic type?
The debugger preview popup shows actual linebreaks as \n so that the preview remains single line. The text visualizer shows linebreaks correctly.
This implies to me that the JSON itself is broken, because newlines should be encoded with \n.
Linebreaks in strings are illegal in Javascript, and thus also in JSON.
If this doesn't happen to the the issue: the nuget version was published 1st Oct, but there's a commit in github dated 3rd Oct with comment "fix whitespace issues hopefully once and for all". Worth trying.