是否可以在 f# 中使用 CallerMemberNameAttribute [英] Is it possible to use CallerMemberNameAttribute in f#

查看:19
本文介绍了是否可以在 f# 中使用 CallerMemberNameAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处注明了属性和 C# 示例,但 FSharp 似乎无法实现.

The attribute and C# examples are noted here but it doesn't look to be possible for FSharp.

http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute.aspx?cs-save-lang=1&cs-lang=csharp#代码片段2

// 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# 现在支持 CallerLineNumberCallerFilePath,但 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆