Why would typeof(Foo) ever return null?

23

Occasionally, I see that typeof(Foo) returns null. Why would this happen?

This is in C#, .NET 3.5.

I thought it might have something to do with the assembly containing the type not yet being loaded, but a test app shows that the assembly is loaded at the start of the method where typeof is used.

Any ideas?


Update 1

  • I can't provide a reproducible sample as this happens on a huge application
  • When I say 'occasionally' I mean in the same method in my application but during various instances. Also, when it fails once when running, it'll fail every time for that instance of the application.

Update 2

The application in question uses a huuuuuge amount of memory and runs on 32bit XP. I'm thinking maybe it's a TypeLoadException or OutOfMemoryException that's somehow being swallowed (but I can't see how, as I've tried this with first-chance exceptions turned on in the debugger).


Update 3

Ran into the same issue just now. Here's the stack trace: enter image description here The code up to this point is literally just:

Type tradeType = typeof(MyTradeType)
TradeFactory.CreateTrade(tradeType)

(before, it was ..CreateTrade(typeof(MyTradeType)) so I couldn't actually tell if the typeof returned null)

So, it looks like typeof() isn't returning null but it's getting set to null by the time it ends up in the CreateTrade method.

The exception (NullReferenceException) has a HResult property of 0x80004003 (Invalid pointer). A call to System.Runtime.InteropServices.Marshal.GetLastWin32Error( ) (in the Immediate Window) returns 127 (The specified procedure could not be found). I've looked in the Modules window and the module that contains this type and method has been loaded and there doesn't look to be any loader errors.


c#
.net
asked on Stack Overflow Jan 20, 2011 by Steve Dunn • edited Jan 24, 2011 by Steve Dunn

5 Answers

4

Has loading the dll failed for some reason? Have you checked the fusion logs.

I'd assume this would cause more problems than just this, but if you're doing this check before using anything from the assembly, it may be hiding any further problems.

answered on Stack Overflow Jan 20, 2011 by Massif
2

Since typeof(T) is a compile time operator the loading-time of the assembly won't be involved.

It would be interesting to see some code that demonstrates this.

Even more interesting to see it happen sometimes and sometimes not.

A first answer might be : use GetType() on an instance.

answered on Stack Overflow Jan 20, 2011 by Henk Holterman
1

typeof determines the type during the compile time. So even if it returns null then it should return null always. Because the behavior does not change during the runtime. Give some code snippet some other stuff is broken.

answered on Stack Overflow Jan 20, 2011 by ferosekhanj • edited Jan 31, 2011 by Greg
1

This is quite possible and very easy to reproduce. typeof(T) will return null if the type has been created in memory. Through System.Reflection.Emit for example.

answered on Stack Overflow Jun 27, 2011 by redb • edited Feb 21, 2017 by redb
1

I was having this problem in my VSPackage project when using typeof(MyClass) in the package's constructor. I moved my code to the overridden Initialize() method and then it worked fine, so it looks like the assembly not being loaded yet may be a factor in this error some times. I'll note too that my VSPackage is loaded at run-time into Visual Studio via MEF, so this likely isn't your typically scenario, but still thought I'd mention it.

answered on Stack Overflow Feb 21, 2014 by deadlydog

User contributions licensed under CC BY-SA 3.0