StrataFrame Forum

Using property as a string to resolve at runtime to an object reference

http://forum.strataframe.net/Topic22125.aspx

By Charles R Hankey - 2/27/2009

I have a piece of code in a form :





Private Sub lvSelections_ListPopulating(ByVal e As MicroFour.StrataFrame.UI.ListPopulatingEventArgs) _

Handles lvSelections.ListPopulating



e.Parameters(0).Value = Me.Vr_PolicyHolderListBO1

e.Parameters(1).Value = _

MicroFour.StrataFrame.Business.BusinessCloneDataType.ClearAndFillFromCompleteTable

End Sub



Private Sub btnViewReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles btnViewReport.Click

UI.FormManager.OpenReport(GetType(InsurtecPH.Reporting.Forms.PolicyHolderListReportForm), _

New Object() {Me.Vr_PolicyHolderListBO1}, False)

End Sub




I want to have properties that can be set at design time that at run-time will result in the BO references being set to the object referred to by the property and ReportForm likewise.



Since the form is in a different assembly and for simplicity sake it seems two string properties would do the trick but I am open to suggestions. Obviously I want to make this generic so that in a subclass I can set the properties and have the same behavior except for the the BO and form used. Struggling with syntax.



Suggestions appreciated. TIA


By Greg McGuffey - 2/27/2009

There is an method in the framework that takes the full name of a type and returns its type (yep, I finally found it!):



Microfour.StrataFrame.Tools.Common.GetTypeFromReferencedAssemblies(typeName As String)




So, if you had a property, say ReportForm, which was a string, then you could redo your code like:



'-- Get the System.Type of the report form

Dim reportFormType As System.Type

reportFormType = Common.GetTypeFromReferencedAssemblies(Me.ReportForm)

UI.FormManager.OpenReport(reportFormType, New Object() {Me.Vr_PolicyHolderListBO1}, False)




It would be harder to do this generically with the BO, as you passing an instance, not a type. So, you'd somehow need to actually get an instance of the correct type. Just knowing the name of the type wouldn't be helpful, as the BO is being passed to the constructor of the report form, and has the data that will actually drive the report. Thus it would need not only be instantiated, but also filled.



However, you could provide a generic BusinessObject property of the more general type BusinessLayer. This would then get set somehow with the actual BO that contained the data. The code would then become:



'-- Get the System.Type of the report form

Dim reportFormType As System.Type

reportFormType = Common.GetTypeFromReferencedAssemblies(Me.ReportForm)

UI.FormManager.OpenReport(reportFormType, New Object() {Me.BusinessObject}, False)




Hope that gets you moving some more! BigGrin