Create a Class at runtime for use with PropertyGrid

C

Chris Dunaway

I have a legacy application that I need to build a Windows Forms
application to interface with it. The legacy application keeps its
configuration information in INI style files.

I wish to use a PropertyGrid on the form to allow the user to adjust the
configuration of the legacy app. The problem I am facing, however, is that
the legacy app can still be changed and new configuration items can be
added/removed from the configuration files of the legacy app.

I would normally just create a class with all the properties of the INI
file but since the INI file might change, I need to be able to dynamically
change that class without having to rebuild the app each time.

I want to read the INI file and dynamically build a class with a property
for each item. I think I'll have a xml file that contains information
about the parameters in the INI file along with their types (integer,
boolean, etc.). When the INI is updated, then the xml file can be updated
to reflect any changes. The Windows app would not need to change.

I am stuck on creating and instanciating the class at runtime.

Any assistance is appreciated.


--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
C

Chris Dunaway

I am stuck on creating and instanciating the class at runtime.

In answer to my own question, I offer the following code. The form has a
TextBox, a button, and a PropertyGrid, all with their default names. Run
the program and type the following code in the textbox:

Class Test
Private _MyProp As Integer

Public Property MyProp() As Integer
Get
Return _MyProp
End Get
Set(ByVal Value As Integer)
_MyProp = Value
End Set
End Property
End Class


'*** CODE BEGINS
Private Sub Button1_Click(...) Handles Button1.Click

Dim VB As New VBCodeProvider
Dim obj As Object

Dim Compiler As ICodeCompiler = VB.CreateCompiler()
Dim cParams As New CompilerParameters

cParams.GenerateExecutable = False
cParams.GenerateInMemory = True
cParams.IncludeDebugInformation = False

Dim cResults As CompilerResults
cResults = Compiler.CompileAssemblyFromSource(cParams, TextBox1.Text)

If cResults.Errors.Count = 0 Then
Dim assyResult As [Assembly] = cResults.CompiledAssembly
Dim aType As Type

aType = assyResult.GetType("Test")

obj = assyResult.CreateInstance(aType.FullName)
PropertyGrid1.SelectedObject = obj
PropertyGrid1.Refresh()
End If

VB.Dispose()

End Sub
'*** CODE ENDS


--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top