简体   繁体   English

如何在C#上下文中引用包含单引号的F#名称?

[英]How can I refer to F# names containing a single quote in a C# context?

This mostly comes up while trying to debug F# code in the Visual Studio debugger, which expects C# syntax: 这主要是在尝试调试Visual Studio调试器中的F#代码时出现的,它需要C#语法:

The debugger does not recognize F# expressions. 调试器无法识别F#表达式。 To enter an expression in a debugger window or a dialog box during F# debugging, you must translate the expression into C# syntax. 要在F#调试期间在调试器窗口或对话框中输入表达式,必须将表达式转换为C#语法。 When you translate an F# expression into C#, make sure to remember that C# uses == as the comparison operator for equality and that F# uses a single =. 将F#表达式转换为C#时,请确保记住C#使用==作为比较运算符的相等性,并且F#使用单个=。

(from http://msdn.microsoft.com/en-us/library/vstudio/ee843932.aspx ) (来自http://msdn.microsoft.com/en-us/library/vstudio/ee843932.aspx

The problem is that I have variables and functions that contain single quote characters, like path' , but single quotes seem not to be valid in names in C#, so entering path' in the debugger window just says Newline expected in constant or words to that effect, because it thinks I am starting a character literal. 问题是我有包含单引号字符的变量和函数,比如path' ,但单引号似乎在C#中的名称中无效,因此在调试器窗口中输入path'只是说Newline expected in constant或单词为效果,因为它认为我正在开始一个字符文字。 Even right-clicking on it and adding a watch via the contextual menu gives a similar error. 即使右键单击它并通过上下文菜单添加手表也会产生类似的错误。

How can I translate F# names containing single quotes into C# syntax, so that I can inspect them from the debugger or refer to them from C# code? 如何将包含单引号的F#名称转换为C#语法,以便我可以从调试器中检查它们或从C#代码中引用它们?

I do not think there is a way to do this - so the best option is probably to rename the variable path' to some other name that is a valid C# identifier like pathAux . 我认为没有办法做到这一点 - 所以最好的选择可能是将变量path'重命名为一个有效的C#标识符,如pathAux

Although F# is more flexible with naming (and allows things like path' or even ``whatever !`` ), it is probably a good idea to use C#-compatible identifier names most of the times, because it will make the code readable to a wider audience - such as C# developers. 虽然F#在命名方面更灵活(并允许像path'这样的东西甚至``whatever !`` ),但是大多数时候使用C#兼容的标识符名称可能是个好主意,因为它会使代码可读更广泛的受众 - 例如C#开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM