Domanda

Con il codice qui sotto, il successo e il fallimento sono compilati in 2 classi separate.Come posso fornire attributi personalizzati per il successo e il fallimento?

type Result<'TSuccess,'TFailure> = 
    | Success of 'TSuccess
    | Failure of 'TFailure
.

Modifica Dal momento che Success e Failure Risultato in classi Ho bisogno di decorarli con attributo compatibile con classe AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface)

Posso farlo funzionare?In caso contrario, perché no?

[<ClassAttribute>]
type Result<'TSuccess,'TFailure> = 
    | [<ClassAttribute>] Success of 'TSuccess
    | [<ClassAttribute>] Failure of 'TFailure
.

È stato utile?

Soluzione

Gli attributi possono essere posizionati tra il | e il nome del caso dell'Unione.

Ecco un semplice esempio

open System
type t =  |[<Obsolete("hello")>]A
.

Altri suggerimenti

Un attributo su un caso sindacale viene associato al metodo statico New<Case> che ritorna la sottoclasse per tale custodia o con la proprietà Case per un caso di valore.Con questa definizione in F # ...

type Result<'TSuccess,'TFailure> = 
    | [<Obsolete>] Success of 'TSuccess
    | Failure of 'TFailure
    | [<Obsolete>] NotSure
.

Usa questo snippet c # per invocare la decompilazione ...

var y = Result<string, string>.NewSuccess("yay");
var z = Result<string, string>.NotSure;
.

Arrestare la definizione di newsuccess porterà la definizione c # del risultato from metadata che mostra l'attributo obsoleto.

[Obsolete]
public static Result<TSuccess, TFailure> NewSuccess(TSuccess item);

[Serializable]
[DebuggerDisplay("{__DebugDisplay(),nq}")]
public class Success : Result<TSuccess, TFailure>
{
  [CompilationMapping(SourceConstructFlags.Field, 0, 0)]
  [CompilerGenerated]
  [DebuggerNonUserCode]
  public TSuccess Item { get; }
}
.

Per un identificatore di caso come nobile sopra che non ha valori, il caso diventa una proprietà piuttosto che una sottoclasse e l'attributo obsoleto allegato alla custodia è compilato in aria sottile per quanto riguarda la decompilazione dei metadati ... Guardando attraverso il lIstasm, gli attributi ci sono tutto bene su entrambi ...

Method #3 (06000003) 
-------------------------------------------------------
MethodName: NewSuccess (06000003)
Flags     : [Public] [Static] [ReuseSlot]  (00000016)
RVA       : 0x00002064
ImplFlags : [IL] [Managed]  (00000000)
CallCnvntn: [DEFAULT]
ReturnType: GenericInst Class Result`2< Var!0, Var!1>
1 Arguments
    Argument #1:  Var!0
1 Parameters
    (1) ParamToken : (08000001) Name : item flags: [none] (00000000)
CustomAttribute #1 (0c000011)
-------------------------------------------------------
    CustomAttribute Type: 0a00000b
    CustomAttributeName: System.ObsoleteAttribute :: instance void .ctor()
    Length: 4
    Value : 01 00 00 00                                      >                <
    ctor args: ()

CustomAttribute #2 (0c000012)
-------------------------------------------------------
    CustomAttribute Type: 0a00000c
    CustomAttributeName: Microsoft.FSharp.Core.CompilationMappingAttribute :: instance void .ctor(value class Microsoft.FSharp.Core.SourceConstructFlags,int32)
    Length: 12
    Value : 01 00 08 00 00 00 00 00  00 00 00 00             >                <
    ctor args: ( <can not decode> )


Method #7 (06000007) 
-------------------------------------------------------
MethodName: get_NotSure (06000007)
Flags     : [Public] [Static] [ReuseSlot]  (00000016)
RVA       : 0x0000208c
ImplFlags : [IL] [Managed]  (00000000)
CallCnvntn: [DEFAULT]
ReturnType: GenericInst Class Result`2< Var!0, Var!1>
No arguments.
CustomAttribute #1 (0c000030)
-------------------------------------------------------
    CustomAttribute Type: 0a00000b
    CustomAttributeName: System.ObsoleteAttribute :: instance void .ctor()
    Length: 4
    Value : 01 00 00 00                                      >                <
    ctor args: ()

CustomAttribute #2 (0c000031)
-------------------------------------------------------
    CustomAttribute Type: 0a00000c
    CustomAttributeName: Microsoft.FSharp.Core.CompilationMappingAttribute :: instance void .ctor(value class Microsoft.FSharp.Core.SourceConstructFlags,int32)
    Length: 12
    Value : 01 00 08 00 00 00 02 00  00 00 00 00             >                <
    ctor args: ( <can not decode> )
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top