If you reference a assembly your app becomes dependent of it, it must be present in order for your app to run. In deployment scenario it must then be included in setup for local libraries ('copy local' setting), or as a prerequisite (something that must be installed first) for external libraries such as the .Net Framework and the AjaxControlToolkit where these libraries are installed to GAC.
It is also possible to use libraries without referencing them at design time, by loading them dynamically at runtime using Reflection. You can then not write any strongly typed code for these libraries, only discover them with the Reflection type system. The most basic example is this, same as the Textbox example only now System.Web assembly is not referenced:
Code:
Dim asm As Reflection.Assembly = Reflection.Assembly.Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
Dim o As Object = asm.CreateInstance("System.Web.UI.WebControls.TextBox")
Me.PropertyGrid1.SelectedObject = o
Reflection and the example above has little interest if you have to write the control names manually, so I include a more advanced example that show how control types can be discovered and listed, and later used to create dynamic instances to be assigned the PropertyGrid. This method loads all public types from System.Web that is derived from WebControl and belongs to System.Web.UI.WebControls namespace, and that has a parameterless contructor, much similar to the controls in the web toolbox probably.
Code:
Private Sub LoadWebControls()
Dim asm As Reflection.Assembly = Reflection.Assembly.Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
Dim webControlType As Type = asm.GetType("System.Web.UI.WebControls.WebControl")
Dim controlTypes As New List(Of Type)
For Each t As Type In asm.GetExportedTypes
If webControlType.IsAssignableFrom(t) AndAlso t.Namespace = "System.Web.UI.WebControls" Then
Dim flags As Reflection.BindingFlags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public
Dim ctor As Reflection.ConstructorInfo = t.GetConstructor(flags, Nothing, Type.EmptyTypes, Nothing)
If ctor IsNot Nothing Then
controlTypes.Add(t)
End If
End If
Next
Me.ListBox1.DataSource = controlTypes
Me.ListBox1.DisplayMember = "Name"
End Sub
When user selects as type in listbox the existing control instance in PropertyGrid is disposed and the new instance is created:
Code:
Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
Dim o As Object = Me.PropertyGrid1.SelectedObject
If o IsNot Nothing Then
CType(o, IDisposable).Dispose()
End If
Dim t As Type = CType(Me.ListBox1.SelectedValue, Type)
o = Activator.CreateInstance(t)
Me.PropertyGrid1.SelectedObject = o
End Sub