9
讓我們看看下面的一段代碼,並假設MyAttribute
和test
函數都不能更改。如何在枚舉中保留值類型
type MyAttribute() =
inherit Attribute()
let mutable prop = null
member this.Prop
with get(): obj = prop
and set(value) = prop <- value
type MyEnum =
| A = 1
| B = 2
[<My(Prop = MyEnum.B)>]
type MyClass = class
end
let test() =
let t = typeof<MyClass>
let a = t.GetCustomAttributes(false).[0] :?> MyAttribute
let e = a.Prop
Convert.ToString(e, Globalization.CultureInfo.CurrentCulture)
我預計test
回報B
但它返回2.生成的IL代碼顯示,大約有枚舉類型信息丟失,並傳遞到屬性值僅有2.
有什麼辦法(我猜它應該是一些屬性)保存屬性值中的類型?什麼在C#中的作品更有趣的等效代碼預期
等效的C#:
class MyAttribute : Attribute
{
public object A { get; set; }
}
enum T { A,B,C }
[My(A = T.A)]
class MyClass
{ }
var a = typeof(MyClass).GetCustomAttributes(false)[0] as MyAttribute;
Convert.ToString(a.A, System.Globalization.CultureInfo.CurrentCulture)
您能分享您提到的等效C#代碼嗎? – MarcinJuraszek
@MarcinJuraszek:編輯 – Novakov
您在F#中使用'false'和C#中的'true'調用'GetCustomAttributes()';不知道這是什麼,但至少有一點差異。 – TeaDrivenDev