Hi,
The following code (packaged in a 'Console Application' Visual Studio project):
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace TestReflection
{
class Program
{
static void Main(string[] args)
{
bool found = false;
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.GetType("System.Diagnostics.Process") != null)
{
found = true;
break;
}
}
Console.WriteLine(found);
Console.ReadKey();
}
}
}
prints 'True' when running it in debug mode (F5), but 'False' when launching it without the debugger (Ctrl-F5). Other classes show similar behavior (System.Text.RegularExpressions.Regex
), others are found in both cases (System.IO.File
).
I'm probably missing something obvious - why is this happenning?
(same thing happens in both Visual Studio 2005 and 2008).
UPDATE
List of assemblies found:
Debug mode:
mscorlib
TestReflection
System
Microsoft.VisualStudio.HostingProcess.Utilities
System.Windows.Forms
Run mode:
mscorlib
TestReflection
As the answers imply, in run mode the System assembly is missing (not loaded). My problem was that I was assuming GetAssemblies() was also returning not loaded assemblies.
While this explains the behavior for System.Diagnostics.Process
, why is my code finding System.IO.File
in both run and debug modes?
Thanks!
UPDATE 2
I've changed the code to loop through the loaded assemblies and the current assembly, collecting a list of assemblies referenced by these. If after iterating the loaded assemblies I can't find the type I'm looking for, I start loading and inspecting the referenced assemblies.
It seems that even if I have a reference to System.dll
in my project (I'm looking at the properties of the 'System' reference in Visual Studio), my executable file only references mscorlib.dll
(according to Reflector).
How do I add a 'real' reference to System.dll
? Placing a dummy line
new System.Diagnostics.ProcessStartInfo();
at the start of Main
does the trick (things work as expected, Reflector shows references for both mscorlib.dll
and System.dll
when inspecting the executable), but it is a hack.
Thanks again!