Skip to content

Enum type is lost when used in attribute #995

Description

@Novakov

Description
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    Projects

    Status
    In Progress

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions