简体   繁体   中英

Initialize instance of internal discriminated union from F# to C#

I have a discriminated union type exported via a dll from F# to be used in C#:

type Argument =
  | IntValue
  | FloatValue
  | BoolValue of bool

In C# I can initialize something like Argument.NewBoolValue(true) , but the other two types IntValue and FloatValue are not type generated via the F# compiler and hence marked as internal, is there a way to initialize them in C#?

If the discriminated union case doesn't have any payload, there is no NewXXX method generated. Because all instances of such union case will be the same and there is no need in creation function. So use staic properties instead

Argument.IntValue
Argument.FloatValue

Internally these properties just return singleton instance of corresponding usecase:

internal static readonly Argument _unique_IntValue = new _IntValue();

public static Argument IntValue => _unique_IntValue;

More interesting details can be observed in this decompiled snippet .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM