区分联合案例的自定义类属性

时间:2014-07-31 00:00:21

标签: f# discriminated-union

使用下面的代码,Success和Failure被编译成2个单独的类。如何为成功和失败提供自定义属性?

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

修改 由于SuccessFailure会产生类,我需要使用类兼容的属性AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface)来装饰它们

我能让这个工作吗?如果没有,为什么不呢?

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

2 个答案:

答案 0 :(得分:3)

union表达式的属性与静态New<Case>方法相关联,返回该情况的子类,或者Case属性与无值情况相关联。在F#中有了这个定义......

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

使用此C#代码段来调用反编译...

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

转到NewSuccess的定义将显示结果from metadata的C#定义,显示过时的属性。

[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; }
}

对于像NotSure这样没有值的案例标识符,case就变成了属性而不是子类,并且只要元数据反编译,就会将附加到case的过时属性编译成空白......

[CompilerGenerated]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[DebuggerNonUserCode]
public static Result<TSuccess, TFailure> NotSure { get; }

通过ILdasm查看,这两个属性都没有...

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> )

答案 1 :(得分:2)

属性可以放在|和联合案例名称之间。

这是一个简单的例子

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