F# 无法编译';编译C#客户端时,在不同的模块中定义
在编译C#客户端时,我无法编译在不同模块中定义的F#类型扩展 Events.fs 我有以下类型:F# 无法编译';编译C#客户端时,在不同的模块中定义,f#,F#,在编译C#客户端时,我无法编译在不同模块中定义的F#类型扩展 Events.fs 我有以下类型: type RegistrationSubmissionEvent = | RegistrationSucceeded of Profile | RegistrationFailed of ValidatedForm EventExtraction.fs 我在一个单独的模块中实现了RegistrationSubmissionEvent的类型扩展: type Registrati
type RegistrationSubmissionEvent =
| RegistrationSucceeded of Profile
| RegistrationFailed of ValidatedForm
EventExtraction.fs
我在一个单独的模块中实现了RegistrationSubmissionEvent的类型扩展:
type RegistrationSubmissionEvent with
member x.TryGetProfile() =
match x with
| RegistrationSucceeded profile -> Some profile
| _ -> None
C#客户:
var viewmodel = new ProfileEditorViewmodel(args.TryGetProfile().Value, SaveProfile());
问题:
var viewmodel = new ProfileEditorViewmodel(args.TryGetProfile().Value, SaveProfile());
问题是,即使我在C代码中导入EventExtraction模块,它仍然无法将TryGetProfile识别为该类型的函数
注意,如果类型扩展与区分的联合类型位于同一模块中,则此代码有效。如果希望扩展方法在C#中可见,则应使用C#样式扩展方法的语法
open System.Runtime.CompilerServices
[<Extension>]
type RegistrationSubmissionEventExtension () =
[<Extension>]
static member TryGetProfile (x : RegistrationSubmissionEvent) =
match x with
| RegistrationSucceeded profile -> Some profile
| _ -> None
open System.Runtime.CompilerServices
[]
类型注册SubmissionEventExtension()=
[]
静态成员TryGetProfile(x:RegistrationSubmissionEvent)=
将x与
|注册成功配置文件->某些配置文件
|无
为了解释为什么将扩展放在同一个模块中时它会起作用,您应该看看上的内部扩展和可选扩展之间的区别,但简言之,如果它们在同一个模块中,扩展方法实际上会添加到类型本身中(因此也可以通过反射获得)。在您的C代码中,您是否也导入了定义扩展方法的名称空间?我导入了,但没有任何区别。如果有帮助的话,我可以录制一段视频来演示这个问题…@FyodorSoikin-实际上,我导入了“模块”,而不是名称空间。也许我应该在名称空间而不是模块下定义类型扩展?我相信在将扩展方法添加到C#之前就存在F#类型扩展功能,这就是为什么现在有两种方法在F#中进行扩展。更新为:type RegistrationSubmissionEventExtension()=