title: How do I obtain a list of Collections which contain Objects which Implement an Interface in C# or VB.net
body: <p>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.</p>
  
  <p>I am working with the Microsoft.SqlServer.Management.Smo Namespace.</p>
  
  <p>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.</p>
  
  <p>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)</p>
  
  <pre><code>    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 &amp; "\" &amp; TypeOf(collection_object).toString() &amp; "\", False)
         End If
      Next
  Next
  </code></pre>
  
  <p>So far I am able to connect to the database and script out Tables by doing the following.</p>
  
  <pre><code>For Each obj As Table In db.Tables
                  If Not (obj.IsSystemObject) Then
                      ScriptObjectCreate(obj, destFolder &amp; "\" &amp; "Tables\", False)
                  End If
  Next
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>For each obj as Table
  For each obj as StoredProcedure 
  For each obj as Trigger
  etc...
  </code></pre>
  
  <p>I would like to enumerate all objects in db with one function if possible</p>