Repository navigation
Open
Labels
Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingArea-Compiler-Checking-AttributesAttribute targets, AllowMultiple, CompiledName, custom attribute encodingBugTriage-Investigate
Milestone
Description
Repro steps
Try building this
namespace TypeOfInAttributeConstructorRepro
open System
open System.ComponentModel
open System.Reflection
open System.Text.Json
open System.Text.Json.Serialization
type EnumDescriptionConverter<'TEnum when 'TEnum: (new: unit -> 'TEnum) and 'TEnum: struct and 'TEnum :> ValueType>() =
inherit JsonConverter<'TEnum>()
override _.Read(reader, typeToConvert, options) =
let value = reader.GetString()
if String.IsNullOrEmpty value then
Unchecked.defaultof<'TEnum>
else
match Enum.TryParse<'TEnum>(value, true) with
| true, result -> result
| false, _ -> raise (JsonException($"Unable to convert '%s{value}' to Enum '%s{typeof<'TEnum>.Name}'."))
override _.Write(writer, value, options) =
let fi = value.GetType().GetField(value.ToString())
let description = fi.GetCustomAttribute<DescriptionAttribute>(false)
writer.WriteStringValue(description.Description)
type [<JsonConverterAttribute (typeof<EnumDescriptionConverter<FileOpenPreference>>)>] FileOpenPreference =
| [<Description "inline">] Inline = 0
| [<Description "desktop">] Desktop = 1
| [<Description "web">] Web = 2Expected behavior
Builds
Actual behavior
Displays 2 weird errors
error FS0001: A generic construct requires that the type 'FileOpenPreference' have a public default constructor
error FS0267: This is not a valid constant expression or custom attribute value
Known workarounds
Derive from attribute
Related information
.NET 8
Metadata
Metadata
Assignees
Labels
Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingArea-Compiler-Checking-AttributesAttribute targets, AllowMultiple, CompiledName, custom attribute encodingBugTriage-Investigate
Type
Projects
- StatusShow more project fieldsNew
Activity