T. Fabre
T. Fabre

Reputation: 1527

Type.GetType for System.Net types fails

I just came across this oddity when trying to instantiate a WebProxy instance through reflection :

    Dim proxyType As Type = GetType(System.Net.WebProxy)
    MsgBox(proxyType.FullName)

    Dim reflProxyType As Type = Type.GetType(proxyType.FullName)
    MsgBox(reflProxyType.FullName) ' Here, reflProxyType is null => NullReferenceException

Changing the first line to other System namespaces (ie. System.Text.StringBuilder or System.String) works fine.

    Dim systemType As Type = GetType(System.Text.StringBuilder)
    MsgBox(systemType.FullName)

    Dim reflSystemType As Type = Type.GetType(systemType.FullName)
    MsgBox(reflSystemType.FullName) ' Here, everything works fine

Is there any reason for this behavior ? Am I missing something ? Did MS set up some restrictions on System.dll ?

Upvotes: 1

Views: 225

Answers (1)

Ani
Ani

Reputation: 113402

The answer is in the MSDN docs for Type.GetType (string)

Parameters

typeName Type: System.String

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

The WebProxy class is in System.dll, not Mscorlib.dll. Therefore, you must either:

  1. Provide the assembly-qualified name instead of just the fully-qualified name. (or)
  2. Use the Assembly.GetType(string) method.

Upvotes: 2

Related Questions