【发布时间】:2011-04-30 05:30:03
【问题描述】:
我想知道我是否可以优化我的应用程序的反射部分。
背景信息:
vb.net windows 应用程序检查数据库记录是否适用于不同的规则。所以我创建了一个接口iRule 和(到目前为止)54 个正在实现它的规则类。它们都属于同一个项目(Rule.dll)。因为人们应该将它们配置为在特定条件下处于活动状态,所以数据库中还有一个表 tabRule 列出了所有这些规则(使用 RuleKey/RuleName 等)。这似乎是多余的,但我还没有看到替代方案。
言归正传:
我创建为当前记录激活的每个规则的实例。因为记录计数平均为 50000,规则计数为 54,所以创建实例非常耗时。您对如何优化以下部分或针对不同方法的建议有任何想法(共享/静态对象?)?
Dim asmRule As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Rule.dll"))
...后来大约 2700000 次...
' Find the rule in the rules assembly'
Dim ruleObj As Rule.IRule = Nothing, found As Boolean = False
For Each typeAsm As System.Type In asmRule.GetTypes
If typeAsm.GetInterface(GetType(Rule.IRule).FullName) IsNot Nothing Then
ruleObj = CType(asmRule.CreateInstance(typeAsm.FullName, True), Rule.IRule)
If ruleObj.GetKey = ruleRow.RuleKey Then 'ruleRow is the db-counterpart of the rule class'
found = True
Exit For
End If
End If
Next
'..... execute rule if found....'
谢谢
【问题讨论】:
标签: .net vb.net reflection interface