9

I just noticed kind of a bug in the function:

Type.GetType("System.Uri");

The return value is null whereas the following functions are working quite well...

Type.GetType("System.string");
Type.GetType("System.bool");
Type.GetType("System.DateTime");

...

Anyone knows, why the returned Type is null?

EDIT: removed Uri double entry...

theMayer
  • 15,456
  • 7
  • 58
  • 90
Daffi
  • 506
  • 1
  • 7
  • 20
  • 12
    confused: you show System.URi in both working and not working – BugFinder Oct 15 '12 at 14:56
  • 2
    I am very surprised that `Type.GetType("System.string");` and `Type.GetType("System.bool");` are working because I am not aware of the existence of such types. – Darin Dimitrov Oct 15 '12 at 14:57
  • Darin, string and bool are shorcuts to types in System namespace. http://msdn.microsoft.com/es-es/library/system.string(v=vs.80).aspx – Oscar Oct 15 '12 at 14:58
  • 6
    @Oscar, I know that. But those are C# specific shortcuts. They are only compile-time aliases. When you are using the `Type.GetType` method you should provide the fully qualified name of the type. `Type.GetType("System.bool")` will never work because such type doesn't exist in the BCL. The correct type name is `System.Boolean`. And `System.String`. So this question here doesn't make any sense. @OP, please clarify your question. – Darin Dimitrov Oct 15 '12 at 15:02
  • @Oscar I thought those were compile-time aliases. The _actual_ type is `System.Boolean` and no `System.bool` actually exists. Same with `System.string`. – Chris Sinclair Oct 15 '12 at 15:02
  • @DarinDimitrov I think the @OP just tried to make an example. `System.bool` and `System.string` don't exist and thus `System.Type.GetType("System.bool")` and `System.Type.GetType("System.string")` return `null`. Although `System.Type.GetType("System.Boolean")` and `System.Type.GetType("System.String")` work fine. – Bamdad Jun 01 '21 at 05:03

5 Answers5

16

The reason that Type.GetType("System.Uri") returns null is that the type is located in system.dll instead of mscorlib.dll. You must use the assembly-qualified name as noted above.

From MSDN:

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.

theMayer
  • 15,456
  • 7
  • 58
  • 90
13

I also hit this problem and realized that, especially in ASP.Net with JIT compilation, I do not always know the Assembly information. I added the following to my ReflectionUtilities class. It is a "sledgehammer to crack a nut" to some degree but it works with both the AssemblyQualifiedName and the basic class FullName. The first basically short-circuits the search of the CurrentDomainAssemblies that must otherwise occur.

    public static Type FindType(string qualifiedTypeName)
    {
        Type t = Type.GetType(qualifiedTypeName);

        if (t != null)
        {
            return t;
        }
        else
        {
            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                t = asm.GetType(qualifiedTypeName);
                if (t != null)
                    return t;
            }
            return null;
        }
    }

Note: Given Reflection performance issues this should not be called inside loops without the assembly qualification if at all possible. Better to access the first item you need, extract the assembly information from that, and procede from there. Not always appropriate but much more efficient (if anything in Reflection can be called efficient:-)).

Alistair

4

try this code:

Uri uri = new Uri("http://test");
Type t = Type.GetType(uri.GetType().AssemblyQualifiedName);

and then u can copy/paste the AssemblyQualifiedName from the type

another method would be:

Type t = typeof(Uri);
x4rf41
  • 5,184
  • 2
  • 22
  • 33
1

Type.GetType(String):

Gets the Type with the specified name, performing a case-sensitive search.

Return Value

Type: System.Type

The type with the specified name, if found; otherwise, null.

So, if you make a typo, your type will not be found and null will be returned. This is not a bug.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

Without additional information, I would guess you're not using the fully-qualified type name. Type.GetType() not only expects a fully qualified type name (i.e. System.String), but also the assembly-qualified name, in case you're trying to load anything other than a currently executing assembly type.

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
  • 2
    I suspect this is it. If you hit `Type.GetType("System.Uri")` it returns null. But if you use `Type.GetType("System.Uri, System")` it works fine. (at least in LinqPad) – Chris Sinclair Oct 15 '12 at 15:06