4

F# に C# の nameof(..) に相当するものはありますか? を参照してください

次の場合に nameof 関数をどのように使用または拡張できますか?

let nameof (q:Expr<_>) = 
    match q with 
    | Patterns.Let(_, _, DerivedPatterns.Lambdas(_, Patterns.Call(_, mi, _))) -> mi.Name
    | Patterns.PropertyGet(_, mi, _) -> mi.Name
    | DerivedPatterns.Lambdas(_, Patterns.Call(_, mi, _)) -> mi.Name
    | _ -> failwith "Unexpected format"

let any<'R> : 'R = failwith "!"

let s = _nameof <@ System.Char.IsControl @> //OK

type A<'a>() = 
    static member MethodWith2Pars(guid:Guid, str:string) = ""
    static member MethodWith2Pars(guid:Guid, ba:byte[]) = ""

let s1 = nameof <@ A<_>.MethodWith2Pars @> //Error  FS0503  A member or object constructor 'MethodWith2Pars' taking 1 arguments is not accessible from this code location. All accessible versions of method 'MethodWith2Pars' take 2 arguments
let s2 = nameof <@ A<_>.MethodWith2Pars : Guid * string -> string @> //Same error

コンパイラは次のエラーを返します。

エラー FS0503 このコードの場所からは、1 つの引数を取るメンバーまたはオブジェクト コンストラクター 'MethodWith2Pars' にアクセスできません。メソッド 'MethodWith2Pars' のすべてのアクセス可能なバージョンは 2 つの引数を取ります

4

2 に答える 2