F#数据类型提供程序-使用字符串变量创建 [英] F# Data Type Provider - Create with string variable
问题描述
在F#中,我具有以下代码
In F# I have the following bit of code
[<Literal>]
let fname=fstFile.FullName.ToString()
type genomeFile = CsvProvider<fname>
其中fstFile是FileInfo类的实例.但是,此操作失败:
Where fstFile is an instance of the FileInfo class. However, this fails with:
This is not a valid constant expression or custom attribute value
如果没有实际的硬编码字符串,我似乎无法创建类型.有人知道如何解决这个问题吗?
And I can't seem to create the type without an actual hard coded string. Does anyone know how to fix this?
推荐答案
CSV类型提供程序的参数必须为常量,以便可以在编译时生成类型(而无需实际评估程序.),您可以在运行时加载具有相同架构的其他文件.
The parameter to the CSV type provider needs to be a constant, so that the types can be generated at compile-time (without actually evaluating the program. However, you can load a different file with the same schema at runtime.
因此,处理此问题的最佳方法是将实际输入之一复制到某个知名位置(例如,与脚本位于同一目录中的sample.csv
),然后在运行时使用实际路径:
So, the best way to handle this is to copy one of your actual inputs to some well-known location (e.g. sample.csv
in the same directory as your script) and then use the actual path at runtime:
// Generate type based on a statically known sample file
type GenomeFile = CsvProvider<"sample.csv">
// Load the data from the actual input at runtime
let actualData = GenomeFile.Load(fstFile.FullName.ToString())
这篇关于F#数据类型提供程序-使用字符串变量创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!