Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET reflection question

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!

like image 340
Miron Brezuleanu Avatar asked Jan 26 '10 12:01

Miron Brezuleanu


1 Answers

AppDomain.GetAssemblies does not return all assemblies that you have referenced, rather it returns all assemblies that are currently loaded into the appdomain.

Clearly, the Diagnostics.Process class is not directly used by your application and will thus not be loaded when running outside the debugger.

So why do we find System.IO.File but not System.Diagnostics.Process? The reason is that the two classes, though they reside in the same top-level namespace System, actually lives in two different assemblies. This is easily seen if you look up the two classes in the Visual Studio Object Browser. The File class happens to live in the mscorlib dll while the Process class lives in the System dll.

Since no .Net application can run without mscorlib that assembly will be loaded, while System.dll is not loaded since you are not referencing any types that lives in that dll.

like image 189
Peter Lillevold Avatar answered Jan 12 '23 00:01

Peter Lillevold