Loading assembly

IfYouSaySo

Well-known member
Joined
Jan 4, 2006
Messages
102
Location
Hermosa Beach, CA
Programming Experience
3-5
Is there some standard way to load an assembly (that is in the GAC)? OK, I know there is Assembly.Load(ByVal name As AssemblyName). But I've been trying to load one of the standard assemblies this way--for example "System"--and it keeps throwing file not found. I read somewhere on the net that in order to load an assembly that is in the GAC, you need the AssemblyName to specify the name, version, and public key. Well...the name and version are easy to come by, but I'm not sure how to come up with the public key. For example, if I go to C:\Windows\Assembly, Explorer shows me the "public key token", but I'm pretty sure that is not the same as the public key. Any ideas??
 
well actually the name you need is:
-.net 2.0: System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
- .net 1.1: System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

So you can use the public key token to load an assembly, I've created a small win app to test it:

VB.NET:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim asm As Reflection.Assembly = Reflection.Assembly.Load("System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
        Dim sb As New System.Text.StringBuilder
        sb.Append("Is Assembly is an instance? --> " & CStr(Not (asm Is Nothing)) & Environment.NewLine)
        sb.Append("Assembly's name is  :" & asm.GetName.Name & Environment.NewLine)
        sb.Append("Assembly's public key = " & Getstring(asm.GetName.GetPublicKey).ToString & Environment.NewLine)
        sb.Append("Assembly's public key token = " & Getstring(asm.GetName.GetPublicKeyToken).ToString & Environment.NewLine)
        Me.TextBox1.Text = sb.ToString
    End Sub

    Private Function Getstring(ByVal b As Byte()) As String
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To (b.GetLength(0)) - 1
            sb.Append(String.Format("{0:x}", b(i)))
        Next i
        Return sb.ToString
    End Function
which will result in the following output:
VB.NET:
Is Assembly is an instance? --> True
Assembly's name is  :System.Web
Assembly's public key = 02400480009400062000240052534131040010107d1fa57c4aed9f0a32e84aafaefdde9e8fd6aec8f87fb3766c834c99921eb23be79ad9d5dcc1dd9ad2361321290b723cf980957fc4e177108fc67774f29e832e92ea5ece4e821c0a5efe8f1645c4cc93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaffc4963d261c8a12436518206dc093344d5ad293
Assembly's public key token = b03f5f7f11d5a3a
 
Thanks, I finally figured that out late yesterday afternoon. I had trouble finding documentation for that particular syntax on MSDN...for example it isn't documented under System.Reflection.Load...the online docs really only seem to cover loading assemblies from the local project...oh well. Thanks for the reply.
 
Back
Top