繁体   English   中英

如何在F#类型的公共方法中添加属性?

[英]How can I add attributes to a public method in an F# type?

我只是第一次尝试F#,并与C#项目进行互操作。

我在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”语句中设置它们,但不能在“成员”语句中设置它们。 我确定这是愚蠢的,但我提前感谢你的帮助!

试试这个:

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

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

问题是您的member定义是属性,而不是方法。 正如在Wesley的回答中,添加一些参数(只是空的parens很好)使它成为一种方法。

(可能也看到了

http://blogs.msdn.com/b/timng/archive/2010/04/05/f-object-oriented-programming-quick-guide.aspx

http://lorgonblog.wordpress.com/2009/02/13/the-basic-syntax-of-f-classes-interfaces-and-members/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM