Can I provide a Type as an input to a Type provider in F#?

前端 未结 1 1546
终归单人心
终归单人心 2021-02-07 12:26

Are there any pitfall I should be aware of doing so ?

Would you know of existing code dealing with the same pb I might encounter ?

Thks

1条回答
  •  遥遥无期
    2021-02-07 12:49

    Unfortunately, you cannot pass types as static parameters to a type provider. The static parameters passed using MyProvider< "first argument", 42 > have to be primitive types (like string, int and similar). I don't see the list anywhere in the documentation, but Type is definitely not supported.

    The problem with doing this is that you could pass it typeof where MyType is declared in the same file as the file that's using the type provider, and so the F# compiler would have to first compile the first part of a file, then invoke the provider and then continue. This sounds possible, but it is probably low-priority for the F# team.

    The best way to pass information about type to a type provider is to give it a type name and then lookup the type using reflection (this will only work if the type is from an already compiled assembly):

    type MyTest = MyTypeProvider<"System.Int32">
    

    0 讨论(0)
提交回复
热议问题