reflection question

  • Thread starter Thread starter D Yee
  • Start date Start date
D

D Yee

Hello,

Can someone explain the following:

I'm trying to use Type.GetType that takes a string parameter which specifies
the type and assembly. For example:

Type t = Type.GetType("System.Drawing.Point, System.Drawing");

My issue is that unless I have System.Drawing.dll in the same directory as
my app, this call fails. I did a gacview to confirm that System.Drawing is
in the GAC. Why cant System.Drawing be loaded from the GAC?

I have similar issues doing:

Activator.CreateInstance("System.Drawing", "System.Drawing.Point")

This call only works if System.Drawing is in the same directory as my app -
it cant get loaded from the GAC
 
For assemblies loaded from GAC, you need to provide the version, culture and
publickeytoken information for the runtime to correctly resolve the
assembly. (This excludes the mscorlib assembly per documentation).

This should work (framework version 1.1):

Type t =
Type.GetType("System.Drawing.Point,System.Drawing,Version=1.0.5000.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a")

Depending on the framework version, the Version and PublicKeyToken could be
different in your case. Also, depending on the culture setting of your
computer, the Culture could be different. Apply appropriate values and it
should work.

hope that helps..
Imran.
 
where does this public TokenKey come from?
I mean how can I know it?
is it from the AssemblyName.KeyPair.PublicKey?
 
Lloyd Dupont said:
where does this public TokenKey come from?
I mean how can I know it?
is it from the AssemblyName.KeyPair.PublicKey?
Nope - you can get it from AssemblyName.GetPublicKeyToken (The public key is
a huge key - I think its 1024 bit value - not absolutely sure. public key
token is only the lat 8 bytes of the public key). The method returns an
array of bytes. The values are Hex values. The publickeytoken also appears
in the value returned by objType.AssemblyQualifiedName (which returns the
assembly name, culture, version and public key token). Here's how you can
convert the array of bytes to the value thats displayed by the
AssemblyQualifiedName property:

Dim bArray() As Byte = objType.Assembly.GetName.GetPublicKeyToken
Dim hexs(bArray.Length - 1) As String
Dim Result As New System.Text.StringBuilder

For i As Integer = 0 To bArray.Length - 1
If CInt(bArray(i)) < 16 Then
hexs(i) = "0" & Hex(bArray(i))
Else
hexs(i) = Hex(bArray(i))
End If
Result.Append(hexs(i))
Next

MessageBox.Show(Result.ToString.ToLower)


hope that helps..
Imran.
 
Back
Top