Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Cannot supply typeof<> to attribute constructor #16344

Open

Description

@xperiandri

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 = 2

https://sharplab.io/#v2:DYLgZgzgNAJiDUAfAdgQwLYFMIAdUGNMACAFQE8dMB5MASWQEEAXJgJwEsAjAVycwGEA9sghtu+JoNYAlTDlaCAsACgVgysiIBlMqMzo1G7br7oAdEPQ5hmZEwCygmJmCHbxvedlhgmCe2E3TR1PMxJMAA8mMwApCEDldXcQ0zDI6LjhMy1MDlRgdgAvVCYA5BUVJgpiAFFkbnQAEWx8DhxS4SFkADdcvlYAHgByEjqGogB3AAt3EbH0ECIACmRMCcXuZHYmIgBaAD4iOfr0AEoiVGQYI9GTxdFWcR3L6+PxkEOANXzuTHJKfZLc4AXhURHBRHYyBmHB2mWQXV6rH6w1uDUBpwqyghREESI4ziIAH0zLJUDAlqxMOTclAiFVKCRBIi+nT1B0RCCwTiIb4dt0fsRgUQqTTWGYAOKYJhaNhQgDmQO5PMhYG0cuQ8rMtAgADluMBgFRWDUrFUiALgL96TNytiVTyAKrIfAzfAAa0wMDMzjAqANkjAqPm+2VPJcEEwYYdRHQJVdRHmYVYZAACqhWJHgydAZbfnSxJhzhNtlNow7EPTHpg6VSIAG9oc6wHyyrK37gJG6UTGyLUOxI8t4TUIoR2mUlgASABEztQnF89MERHwwiROyGAFIIABvPOYAC+QyXiZOR23O4ZmEEQbe6H2Zl1GEPQzM09OmNU9vBeNyBOIJIAOqwpgSwTCBrB0vubLjsIEBct+PJ8kQYDsEQwr7pK0r/KBpxYUwABi7AuBSmFMrKHCakCn4xshzgQK07CwZowqofh/DcKIgjoMwco8HwAzNAxbQcrxHD8ZggIdpGNEOuB2y5GYwEKRRCrfFaoH0YxzFmEJ2kcp+lTVEQADaAzwiyyK5GJXC8MQSxXjeAzzHpIllJZKJEb4VAaKmVJgLktiEPs+ynPsAC6RBedQvn+YFLpCmGlZma5TEckQ05QgUqzThFRD0NlQpEAADElpmCS0bnCBl9HupIOC5ZFQl1eo6FEAAjGVKWVWlZQZRMmCcI1RCAYNbUAEwqEAA=

Expected 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

Activity

  1. added this to the Backlog milestone on Nov 27, 2023
  2. added
    Area-Compiler-Checking
    and removed on Aug 19, 2024
  3. added
    Area-Compiler-Checking-Attributes
    on Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-CheckingArea-Compiler-Checking-AttributesBugTriage-Investigate

    Type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions