FSharp.Core


UnionCaseInfo Type

Represents a case of a discriminated union type

Instance members

Instance member Description

this.DeclaringType

Full Usage: this.DeclaringType

Returns: Type

The type in which the case occurs.

Returns: Type
Example

 type Weather = Rainy | Sunny

 let rainy =
     typeof<Weather>
     |> FSharpType.GetUnionCases
     |> Array.head

 rainy.DeclaringType
Evaluates to a value of type System.Type that holds type information for Weather.

this.GetCustomAttributes

Full Usage: this.GetCustomAttributes

Parameters:
    attributeType : Type - The type of attributes to return.

Returns: obj[] An array of custom attributes.

Returns the custom attributes associated with the case matching the given attribute type.

attributeType : Type

The type of attributes to return.

Returns: obj[]

An array of custom attributes.

Example

 type Signal(signal: string) =
    inherit System.Attribute()
    member this.Signal = signal

 type Answer =
     | [<Signal("Thumbs up")>] Yes
     | [<Signal("Thumbs down")>] No

 typeof<Answer>
 |> FSharpType.GetUnionCases
 |> Array.map (fun x -> x.GetCustomAttributes(typeof<Signal>))
Evaluates to
 [|[|FSI_0147+Signal {Signal = "Thumbs up";
                      TypeId = FSI_0147+Signal;}|];
   [|FSI_0147+Signal {Signal = "Thumbs down";
                      TypeId = FSI_0147+Signal;}|]|]

this.GetCustomAttributes

Full Usage: this.GetCustomAttributes

Returns: obj[] An array of custom attributes.

Returns the custom attributes associated with the case.

Returns: obj[]

An array of custom attributes.

Example

 type Weather =
     | Rainy
     | Sunny

 typeof<Weather>
 |> FSharpType.GetUnionCases
 |> Array.map (fun x -> x.GetCustomAttributes())
Evaluates to
 [|[|Microsoft.FSharp.Core.CompilationMappingAttribute
     {ResourceName = null;
      SequenceNumber = 0;
      SourceConstructFlags = UnionCase;
      TypeDefinitions = null;
      TypeId = Microsoft.FSharp.Core.CompilationMappingAttribute;
      VariantNumber = 0;}|];
 [|Microsoft.FSharp.Core.CompilationMappingAttribute
     {ResourceName = null;
      SequenceNumber = 1;
      SourceConstructFlags = UnionCase;
      TypeDefinitions = null;
      TypeId = Microsoft.FSharp.Core.CompilationMappingAttribute;
      VariantNumber = 0;}|]|]

this.GetCustomAttributesData

Full Usage: this.GetCustomAttributesData

Returns: IList<CustomAttributeData> An list of custom attribute data items.

Returns the custom attributes data associated with the case.

Returns: IList<CustomAttributeData>

An list of custom attribute data items.

Example

 type Signal(signal: string) =
   inherit System.Attribute()
   member this.Signal = signal

 type Answer =
     | [<Signal("Thumbs up")>] Yes
     | [<Signal("Thumbs down")>] No

 let answerYes =
     typeof<Answer>
     |> FSharpType.GetUnionCases
     |> Array.find (fun x -> x.Name = "Yes")

 answerYes.GetCustomAttributesData()
Evaluates to
  [|[FSI_0150+Signal("Thumbs up")]
      {AttributeType = FSI_0150+Signal;
       Constructor = Void .ctor(System.String);
       ConstructorArguments = seq ["Thumbs up"];
       NamedArguments = seq [];};
    [Microsoft.FSharp.Core.CompilationMappingAttribute((Microsoft.FSharp.Core.SourceConstructFlags)8, (Int32)0)]
      {AttributeType = Microsoft.FSharp.Core.CompilationMappingAttribute;
       Constructor = Void .ctor(Microsoft.FSharp.Core.SourceConstructFlags, Int32);
       ConstructorArguments = seq
                                [(Microsoft.FSharp.Core.SourceConstructFlags)8;
                                 (Int32)0];
       NamedArguments = seq [];}|]

this.GetFields

Full Usage: this.GetFields

Returns: PropertyInfo[] The fields associated with the case.

The fields associated with the case, represented by a PropertyInfo.

Returns: PropertyInfo[]

The fields associated with the case.

Example

 type Shape =
     | Rectangle of width : float * length : float
     | Circle of radius : float
     | Prism of width : float * float * height : float

 typeof<Shape>
 |> FSharpType.GetUnionCases
 |> Array.map (fun unionCase ->
     unionCase.GetFields()
     |> Array.map (fun fieldInfo ->
         fieldInfo.Name,
         fieldInfo.PropertyType.Name))
Evaluates to
 [|[|("width", "Double"); ("length", "Double")|];
   [|("radius", "Double")|];
   [|("width", "Double"); ("Item2", "Double"); ("height", "Double")|]|]

this.Name

Full Usage: this.Name

Returns: string

The name of the case.

Returns: string
Example

 type Weather = Rainy | Sunny

 typeof<Weather>
 |> FSharpType.GetUnionCases
 |> Array.map (fun x -> x.Name)
Evaluates to [|"Rainy", "Sunny"|]

this.Tag

Full Usage: this.Tag

Returns: int

The integer tag for the case.

Returns: int
Example

 type CoinToss = Heads | Tails

 typeof<CoinToss>
 |> FSharpType.GetUnionCases
 |> Array.map (fun x -> $"{x.Name} has tag {x.Tag}")
Evaluates to [|"Heads has tag 0"; "Tails has tag 1"|]