I am new to .Net Programming and would like to know if Reflection is suitable for traversing an Object to discover Collections within an instance of a class.
I am working with the Microsoft.SqlServer.Management.Smo Namespace.
The plan is to connect to a database, determine what collections exists, then for each of those collections, if the Collection exposes objects which implement the IScriptable Interface, generate a script for the object.
How do i do the following (This is pseudo-code as I am seeking assistance with using reflection or some other method to do the following)
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Smo.Agent
Imports Microsoft.SqlServer.Management.Common
Dim db as Database
...
' 1. How do I determine all Collections in the db Object I have created, which implement the IScriptable Interface.
For each myCollection as Collection in db.Database.?Collections?
For each collection_object in myCollection
If collection_object.GetInterface("IScriptable") IsNot Nothing Then
ScriptObjectCreate(collection_object, destFolder & "\" & TypeOf(collection_object).toString() & "\", False)
End If
Next
Next
So far I am able to connect to the database and script out Tables by doing the following.
For Each obj As Table In db.Tables
If Not (obj.IsSystemObject) Then
ScriptObjectCreate(obj, destFolder & "\" & "Tables\", False)
End If
Next
The reason I want to change this, is different versions of SQL Server will contain different collections of objects. Instead of writing code for each type.
For each obj as Table
For each obj as StoredProcedure
For each obj as Trigger
etc...
I would like to enumerate all objects in db with one function if possible