2011-01-22 32 views
3

我只是第一次嘗試使用F#,並且玩弄了與C#項目的互操作。如何將屬性添加到F#類型的公共方法中?

我在C#項目中定義了自定義屬性,並在我的新F#項目中添加了對該項目的引用。當我嘗試將屬性定義爲「let」語句時,「let」是永久私有的,所以屬性無關緊要。當我嘗試將「let」語句改爲別的東西時,比如「member」,我被告知屬性不能定位對象。我應該如何定義函數,如果我希望它是公共的並且具有屬性?

module FSharp 

type public MyClass() = 
    class 
     //this one will get the attributes, but is always private and can't be made public 
     [<Core.NameAttribute("F# name")>] 
     [<Core.DescriptionAttribute("F# desc")>] 
     let f1 = "test from F#" 

     //this one won't get the attributes, but is public and does return the value 
     member this.Function1 = f1 
    end 

正如我在代碼中所述,我似乎有一個問題。我的屬性只設置爲目標方法,我可以將它們設置爲「let」語句,而不是「成員」語句。我相信這是愚蠢的,但我提前感謝你的幫助!

回答

3

試試這個:

type myAttribute() = 
    inherit System.Attribute() 
    ; 

type test() = 
    [<myAttribute()>] 
    member this.func() = "test from f#" 
相關問題