我有一个非常简单的类型提供者;所有类型都被删除,提供的类型具有2000 int readonly属性Tag1..Tag2000
let ns = "MyNamespace"
let asm = Assembly.GetExecutingAssembly()
let private newProperty t name getter isStatic = ProvidedProperty(name, t, getter, isStatic = isStatic)
let private newStaticProperty t name getter = newProperty t name (fun _ -> getter) true
let private newInstanceProperty t name getter = newProperty t name (fun _ -> getter) false
let private addStaticProperty t name getter (``type``:ProvidedTypeDefinition) = ``type``.AddMember (newStaticProperty t name getter); ``type``
let private addInstanceProperty t name getter (``type``:ProvidedTypeDefinition) = ``type``.AddMember (newInstanceProperty t name getter); ``type``
[<TypeProvider>]
type TypeProvider(config : TypeProviderConfig) as this =
inherit TypeProviderForNamespaces(config)
let provider = ProvidedTypeDefinition(asm, ns, "Provider", Some typeof<obj>, hideObjectMethods = true)
let tags = ProvidedTypeDefinition(asm, ns, "Tags", Some typeof<obj>, hideObjectMethods = true)
do [1..2000] |> Seq.iter (fun i -> addInstanceProperty typeof<int> (sprintf "Tag%d" i) <@@ i @@> tags |> ignore)
do provider.DefineStaticParameters([ProvidedStaticParameter("Host", typeof<string>)], fun name args ->
let provided = ProvidedTypeDefinition(asm, ns, name, Some typeof<obj>, hideObjectMethods = true)
addStaticProperty tags "Tags" <@@ obj() @@> provided |> ignore
provided
)
do this.AddNamespace(ns, [provider; tags])
然后是一个测试项目,在两个单独的文件中包含两个模块:
module Common
open MyNamespace
type Provided = Provider<"">
let providedTags = Provided.Tags
type LocalTags() =
member this.Tag1 with get() : int = 1
member this.Tag2 with get() : int = 2
.
.
member this.Tag1999 with get() : int = 1999
member this.Tag2000 with get() : int = 2000
let localTags = LocalTags()
module Tests
open Common
open Xunit
[<Fact>]
let ProvidedTagsTest () =
Assert.Equal<int>(providedTags.Tag1001, 1001)
[<Fact>]
let LocalTagsTest () =
Assert.Equal<int>(localTags.Tag100, 100)
一切都按预期工作(包括测试执行)。我遇到的问题是Visual Studio中的设计时行为,而我编写代码。我期望由于类型提供商而有一些开销,但是缓慢似乎过于坦率。下面报告的时间以秒为单位,指的是按下点(。)键直到智能感知属性列表出现在屏幕上的时间
如果我注释掉或删除第一个测试代码行(以便消除对提供的东西的任何引用),那么我得到
如果属性数量较大,则时间似乎呈指数增长,而不是线性增加,因此在10000时它会变为分钟。
问题是:
如果有人对我为什么需要这么多属性感到好奇,我正在尝试为数据分析师提供一种工具,以便他们可以编写F#脚本并从其架构中包含10000多个标记的历史数据库中获取数据。 / p>
答案 0 :(得分:0)