可能使用CallerMemberNameAttribute在F#

可能使用CallerMemberNameAttribute在F#

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

问题描述

的属性和C#的例子在这里指出,但它不看为FSharp是可能的。

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

<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute.aspx?cs-save-lang=1&cs-lang=csharp#$c$c-snippet-2\">http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute.aspx?cs-save-lang=1&cs-lang=csharp#$c$c-snippet-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:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
source line number: 31

是否有可能做上述F#中若有什么记号?

Is it possible to do the above in F# and if so what is the notation?

推荐答案

通过显示名称 CallerMemberName 不会在code出现在任何地方,所以我认为不支持此功能。 (当然您可以标记与属性的参数,但这些属性是特殊的 - 他们指示编译器,而不是被发现,并在运行时的一些方式使用。)

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月底, 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#).

这篇关于是否有可能使用CallerMemberNameAttribute在F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:06