Here's a recursive one that does circular reference checking, as well.


/
private static void GetReferences(Assembly asm, Dictionary<string, List<string>> referencesList)
       {
           List<string> refs = null;
           if (!referencesList.ContainsKey(asm.FullName))
           {
                refs = new List<string>();
               referencesList.Add(asm.FullName, refs);
           }
           else
           {
//Console.WriteLine("Already Validated References for {0}", asm.FullName);
               return;
           }

           AssemblyName[] references = asm.GetReferencedAssemblies();
           foreach (AssemblyName reference in references)
           {
               refs.Add(reference.FullName);

               //check for circular refrences
               List<string> refsRefs = null;
               if (referencesList.ContainsKey(reference.FullName))
               {
                   refsRefs = referencesList[reference.FullName];
               }

               if (refsRefs != null && refsRefs.Contains(asm.FullName))
               {
//Console.WriteLine("Circular reference between {0} and {1}", asm.FullName, reference.FullName);
                   return;
               }


               Assembly refAsm = null;
               try
               {
                   refAsm = Assembly.Load(reference.FullName);
               }
               catch (FileNotFoundException)
               {
Console.WriteLine("Assembly {0} reference to {1} not found", asm.FullName, reference.FullName);
               }

               if (refAsm != null)
               {
//Console.WriteLine("Assembly {0} references {1}", asm.FullName, reference.FullName);
                   GetReferences(refAsm, referencesList);
               }
           }
       }
/
Frans Bouma wrote:
Try this:

List<string> asmNames = new List<string>();
// If called by a method defined in a class in FooBar
foreach (Assembly asm in
System.Reflection.Assembly.GetCallingAssembly().GetReferencedAssemblies(
))
   asmNames.Add(asm.FullName);

        That won't work for 100%.

        UI.exe calls BL.dll which references foo.dll
        UI.exe won't find 'foo.dll' using your method. You therefore need to
loop through the assemblies recursively. You can also not use the appdomain's
loaded assembly list, as .NET will only load all assemblies which are actively
used.

        For the dependency injection feature in the upcoming llblgen pro v2.5
I had to implement this and ran into this same issue.

        The discovery is also quite time consuming (hence the fact that .net
only loads the dlls which are actively used), and you have to do it in a
separate appdomain to be able to unload everything if you want to.

        It's wise to setup a filter so you can weed out assemblies which
you're not interested in (e.g. framework assemblies) or if you want to build
another reflector, discover per treelevel, so you don't have to run the
discovery recursively. :)

                FB


------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------




// Kristofer

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Knowlton,
Gerald F.
Sent: den 2 juli 2007 17:25
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Looking for technique

Greeting all;

    I'm looking for some pointers (clues for me) on how to discover what
DLL's a windows application will use when executed.

For example;

I have an application called FooBar. What I want to do is to be able
(through code embedded in the application) to find the name(s) of all
the dll(s) that FooBar will call when it is executed and place those
names in a collection.

The Dll's that I am interested in are the ones I made a reference to in
the source code.

I was first thinking "Reflection", but it doesn't appear so after some
investigation.

Perhaps I should investigation deeper ???

I just know this is probably very easy to do and I'm going to kick
myself once someone gives me a clue.


Best regards,


Jerry Knowlton
View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor.  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to