是否可以在 f# 中使用 CallerMemberNameAttribute [英] Is it possible to use CallerMemberNameAttribute in f#
问题描述
此处注明了属性和 C# 示例,但 FSharp 似乎无法实现.
The attribute and C# examples are noted here but it doesn't look to be possible for FSharp.
// using System.Runtime.CompilerServices
// using System.Diagnostics;
public void DoProcessing()
{
TraceMessage("Something happened.");
}
public void TraceMessage(string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Trace.WriteLine("message: " + message);
Trace.WriteLine("member name: " + memberName);
Trace.WriteLine("source file path: " + sourceFilePath);
Trace.WriteLine("source line number: " + sourceLineNumber);
}
样本输出:
message: Something happened.
member name: DoProcessing
source file path: c:UsersusernameDocumentsVisual Studio 2012ProjectsCallerInfoCSCallerInfoCSForm1.cs
source line number: 31
是否可以在 F# 中执行上述操作,如果可以,表示法是什么?
Is it possible to do the above in F# and if so what is the notation?
推荐答案
快速搜索编译器源代码 显示名称 CallerMemberName
没有出现在代码的任何地方,所以我认为不支持此功能.(您当然可以用属性标记参数,但这些属性是特殊的 - 它们指示编译器而不是在运行时以某种方式被发现和使用.)
A quick search through the compiler source code shows that the name CallerMemberName
does not appear anywhere in the code, so I think this feature is not supported. (You can certainly mark a parameter with the attribute, but these attributes are special - they instruct the compiler instead of being discovered and used in some way at runtime.)
2016 年 7 月更新:截至 6 月下旬,F# 现在支持 CallerLineNumber
和 CallerFilePath
,但 CallerMemberName
仍然不存在.不幸的是,似乎特别是一个更难实施.
Update July 2016: As of late June, F# now supports CallerLineNumber
and CallerFilePath
, but CallerMemberName
is still absent. It seems like that one in particular is more difficult to implement, unfortunately.
在相关说明中,F# 有一个一些特殊标识符让您获得当前的源文件名和行号,因此您可以使用 __SOURCE_DIRECTORY__
和 __LINE__
获得类似的信息(但不是像 C# 那样来自调用者).
On a related note, F# has a few special identifiers that let you get the current source file name and line number, so you might be able to get similar information with __SOURCE_DIRECTORY__
and __LINE__
(but not from the caller as in C#).
这篇关于是否可以在 f# 中使用 CallerMemberNameAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!