Enum type is lost when used in attribute
See original GitHub issueDescription
Enum type is lost (casted to int32) when used in attribute named argument of type obj
.
http://stackoverflow.com/questions/35732830/how-to-preserve-value-type-in-enum
Repro Steps
Create attribute with named argument of type obj
. Add that attribute and assign enum value to named argument.
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)
Expected
test
function should return "B"
. In IL value should be stored as enum.
Actual
test
function returns 2
. In IL value is stored as int32 - enum type is lost
Version: Microsoft (R) F# Compiler version 14.0.23413.0
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
typescript - Enum type not defined at runtime
I would understand it if I had used the declare keyword, telling the compiler that something should be available at runtime, but in...
Read more >Using Enums and Custom Attributes
The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator...
Read more >Enumeration Types in Data Contracts - WCF
One way to use enumeration types in the data contract model is to apply the DataContractAttribute attribute to the type.
Read more >enum — Support for enumerations
Base class for creating enumerated constants that can be combined using the bitwise operators without losing their IntFlag membership. IntFlag members are ...
Read more >Using EnumAttr : missing or conflicting information - MLIR
There seems to be conflicting or missing information on how EnumAttr should be used successfully. I read through whatever code and write-ups ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, it can be considered a bug, though it’s fix might need an RFC
The translation from Enums to constants is done too early. IIRC the only reason I didn’t fix it was a concern that quotations actually see the constant, not the field, and because it would mean storing more information in the TAST which we have to do very carefully w.rt. compat.
We can fix this, but it may need a new quotation node that refines “Const” - that is, the node will continue to match “BasicPatterns.Const” but there is an additional “BasicPatterns.EnumConst” or something that sees this refined information.
@dsyme to comment on the labeling, I personally think it’s a bug