我不知道如何通过类型提供程序提供强类型类型。我看到的所有示例都有相同的类型,与输入无关。
MiniCsvTypeProvider只提供双倍。RegexTypeProvider只提供匹配。
是否可以根据提供给类型提供程序的参数提供不同类型的属性(取决于)?
if somevariable then
proptype = typeof<int>
else
proptype = typeof<string>
let staticProp = ProvidedProperty(propertyName = "property",
propertyType = propType,
GetterCode= (fun [arg] -> <@@ %%arg :?> propType @@>))发布于 2012-02-28 15:10:52
是的,根据输入很容易提供不同的类型。作为一个非常简单的例子,您可以这样做:
let propType, propValue =
if somevariable then
typeof<int>, <@@ 1 @@>
else
typeof<string>, <@@ "test" @@>
let prop = ProvidedProperty("property", propType, GetterCode = fun [_] -> propValue)为了按照您建议的方式展开这一点,您可以在条件中定义整个getter:
let propType, propGetter =
if somevariable then
typeof<int>, fun [arg] -> <@@ %%arg : int @@>
else
typeof<string>, fun [arg] -> <@@ %%arg : string @@>
let prop = ProvidedProperty("property", propType, GetterCode = fun [_] -> propValue)但是,请注意,然后需要确保调用属性的表示形式分别是int或string。还请注意,与您选择的命名(staticProp)不同,这些不是静态属性,因为您将接收方(arg)传递给getter,并且没有标记ProvidedProperty静态。
https://stackoverflow.com/questions/9479375
复制相似问题