구별된 공용체 케이스에 대한 사용자 정의 클래스 속성
-
21-12-2019 - |
문제
아래 코드를 사용하면 성공과 실패가 2개의 별도 클래스로 컴파일됩니다.성공 및 실패에 대한 사용자 정의 속성을 제공하려면 어떻게 해야 합니까?
type Result<'TSuccess,'TFailure> =
| Success of 'TSuccess
| Failure of 'TFailure
편집하다부터 Success
그리고 Failure
클래스가 생성되면 클래스 호환 속성으로 장식해야 합니다. AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface)
이 작업을 수행할 수 있나요?그렇지 않다면 왜 안 됩니까?
[<ClassAttribute>]
type Result<'TSuccess,'TFailure> =
| [<ClassAttribute>] Success of 'TSuccess
| [<ClassAttribute>] Failure of 'TFailure
해결책
속성은 |
그리고 노조 사건의 이름.
다음은 간단한 예입니다.
open System
type t = |[<Obsolete("hello")>]A
다른 팁
통합 사례의 속성은 정적과 연결됩니다. 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의 정의로 이동하면 Result의 C# 정의가 표시됩니다. from metadata
더 이상 사용되지 않는 속성을 표시합니다.
[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와 같은 사례 식별자의 경우 사례는 하위 클래스가 아닌 속성이 되며 사례에 첨부된 사용되지 않는 속성은 메타데이터 디컴파일이 진행되는 한 허공으로 컴파일됩니다.
[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> )
제휴하지 않습니다 StackOverflow