[英]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#:我有一个通过 dll 从 F# 导出的可区分联合类型,用于 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#?在 C# 中,我可以初始化类似
Argument.NewBoolValue(true)
的东西,但是其他两种类型IntValue
和FloatValue
不是通过 F# 编译器生成的类型,因此标记为内部,有没有办法在 Z623DFD7EFA19FBE247 中初始化它们?
If the discriminated union case doesn't have any payload, there is no NewXXX
method generated.如果判别联合案例没有任何有效载荷,则不会生成
NewXXX
方法。 Because all instances of such union case will be the same and there is no need in creation function.因为这种联合案例的所有实例都是相同的,并且不需要创建 function。 So use staic properties instead
所以改用静态属性
Argument.IntValue
Argument.FloatValue
Internally these properties just return singleton instance of corresponding usecase:在内部,这些属性只返回相应用例的 singleton 实例:
internal static readonly Argument _unique_IntValue = new _IntValue();
public static Argument IntValue => _unique_IntValue;
More interesting details can be observed in this decompiled snippet . 在这个反编译的代码片段中可以观察到更多有趣的细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.