为什么F#在C#中不允许多个属性?(Why does F# not allow multiple attributes where C# does?)

编程入门 行业动态 更新时间:2024-10-26 12:24:51
为什么F#在C#中不允许多个属性?(Why does F# not allow multiple attributes where C# does?)

以下代码在C#中编译:

[ContentType("text")] [ContentType("projection")] public class Class1 { }

F#中的以下代码不能编译:

[<ContentType("text")>] [<ContentType("projection")>] type Class1() = class end

F#中的编译错误是: “属性类型'ContentTypeAttribute'具有'AllowMultiple = false'。该属性的多个实例不能附加到单个语言元素。”

通过反编译ContentType,我可以看到ContentType继承自AttributeUsage中'AllowMultiple = true'的 MultipleBaseMetadataAttribute。

事实上,似乎F#不会继承父类的AttributeUsage。

[<AttributeUsage(AttributeTargets.Class, AllowMultiple = true)>] type FooAttribute() = inherit Attribute() type BarAttribute() = inherit FooAttribute() [<Foo>] [<Foo>] type MyClassCompiles() = class end

哪里

[<Bar>] [<Bar>] type MyClassDoesNotCompile() = class end

The following code compiles in C#:

[ContentType("text")] [ContentType("projection")] public class Class1 { }

The following code in F# does not compile:

[<ContentType("text")>] [<ContentType("projection")>] type Class1() = class end

The compile error in F# is: "The attribute type 'ContentTypeAttribute' has 'AllowMultiple=false'. Multiple instances of this attribute cannot be attached to a single language element."

By decompiling ContentType, I can see that ContentType inherits from MultipleBaseMetadataAttribute which has 'AllowMultiple=true' in the AttributeUsage.

In fact, it seems like F# does not inherit the AttributeUsage from the parent class.

[<AttributeUsage(AttributeTargets.Class, AllowMultiple = true)>] type FooAttribute() = inherit Attribute() type BarAttribute() = inherit FooAttribute() [<Foo>] [<Foo>] type MyClassCompiles() = class end

where

[<Bar>] [<Bar>] type MyClassDoesNotCompile() = class end

最满意答案

看起来像一个错误。 电子邮件fsbugs [at] microsoft.com。 这是另一个显而易见的错误:它看起来并不尊重AttributeTargets :

[<AttributeUsage(AttributeTargets.Enum)>]
type FooAttribute() = 
  inherit Attribute()

[<Foo>]
type T = struct end //happily compiles

Looks like a bug. Email fsbugs[at]microsoft.com. Here's another apparent bug: it doesn't appear to honor AttributeTargets:

[<AttributeUsage(AttributeTargets.Enum)>]
type FooAttribute() = 
  inherit Attribute()

[<Foo>]
type T = struct end //happily compiles

                    
                     
          

更多推荐

本文发布于:2023-08-07 12:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464289.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   中不   属性   attributes   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!