VB.NET动态工厂

时间:2011-02-12 20:48:49

标签: vb.net

如何更改此代码以使工厂动态化。我相信我接近远程。我不想要工厂的任何硬编码,也不想重新编译。数据库驱动它。

Dim genericRule As Rule
Dim factoryObject As String

factoryObject = _dal.GetFactoryObject(ruleId)

'Close, but not quite will keep working on it.
'Dynamic object creation using remoting to the local computer.
'Dim mArgs(1) As Object
'mArgs(0) = ruleid
'mArgs(1) = pronumber
'Dim mObj As Object
'Try
'    mObj = Activator.CreateInstance(Nothing, "DedicatedBilling.RuleFactory." + mFactoryObject, mArgs)
'    mRule = CType(mObj, Rule)
'Catch ex As Exception
'    mObj = Activator.CreateInstance(Nothing, "DedicatedBilling.RuleFactory.ErrorRule", mArgs)
'    mRule = CType(mObj, Rule)
'End Try

Select Case factoryObject
    Case "DeadHeadEntryRule"
        genericRule = New DeadHeadEntryRule(ruleId, proNumber)
    Case "DeadHeadFuelRateRule"
        genericRule = New DeadHeadFuelRateRule(ruleId, proNumber)
    Case "DeliveryStatusRule"
        genericRule = New DeliveryStatusRule(ruleId, proNumber)

    Case Else
        genericRule = New ErrorRule(ruleId, proNumber)
End Select

1 个答案:

答案 0 :(得分:0)

请不要发布这么多代码。我一开始甚至没有看到代码的重要部分。您无需列出每条规则,以便了解您的观点。

无论如何,你想看看Activator.CreateInstanceFrom和反思,而不是在远程处理。

' Create an instance of the SomeType class that is defined in this assembly.
Dim oh As System.Runtime.Remoting.ObjectHandle = _
    Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase, _
                                 GetType(SomeType).FullName)

' Call an instance method defined by the SomeType type using this object.
Dim st As SomeType = CType(oh.Unwrap(), SomeType)

st.DoSomething(5)