Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2007

Tip: Looking for answers? Try searching our database.

Programatically disabling lazy loading of assemblies

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edgile - 06 Jul 2007 15:32 GMT
Hi,

I would like to enforce loading all referenced assemblies at start up in
order to investigate them via Reflection. However, those referenced
assemblies are loaded by the framework only on demand. Here is a little
example:

namespace TestApp
{
   class Program
   {
       static void Main(string[] args)
       {
           Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
           Console.WriteLine(assemblies.Length);
           object o = Activator.CreateInstance("ClassLibrary1",
"ClassLibrary1.Class1");
           assemblies = AppDomain.CurrentDomain.GetAssemblies();
           Console.WriteLine(assemblies.Length);
       }
   }
}

The ClassLibrary1 is a precompiled assembly that is referenced by this
executable.

Before invoking Activator.CreateInstance, ClassLibrary1 is not loaded, it
can be clearly seen from the assemblies.Length property.

Is there a way that I can force the CLR to load all referenced assemblies
somehow?
Marc Gravell - 06 Jul 2007 15:51 GMT
Just walk them... watch out for the cyclic reference at the bottom ;-p

   static void Main() {
       List<string> considered = new List<string>();
       EnumerateAssemblies(Assembly.GetEntryAssembly(), considered);
   }

   static void EnumerateAssemblies(Assembly assembly, List<string>
considered) {
       foreach (AssemblyName name in
assembly.GetReferencedAssemblies()) {
           try {
               string sName = name.FullName;
               if (considered.Contains(sName)) continue;
               considered.Add(sName);
               Assembly refAssembly = Assembly.Load(name);
               if (refAssembly != null) {
                   EnumerateAssemblies(refAssembly, considered);
               }
           } catch {} // lazy swallow... best endeavors
       }
   }

Marc
Edgile - 06 Jul 2007 22:20 GMT
Hello Marc,

This was quite a fast response. Thanx! However, it seems to me that it does
not work. Here is the code:

namespace ConsoleApplication1
{
   class Program
   {
    static void Main(string[] args)
    {
      Assembly assembly = Assembly.GetEntryAssembly();
      AssemblyName[] refs = assembly.GetReferencedAssemblies();
      Console.WriteLine(refs.Length);
    }
    }
}

This application references some .NET libraries (by default System.Xml,
System.Data) including my own ClassLibrary1.dll. However, the resulted
AssemblyName array contains one single element: mscorlib
Is this a bug in the framework or do I do something wrong?
Marc Gravell - 07 Jul 2007 22:30 GMT
> the resulted AssemblyName array contains one single element

Interesting; I've just done a test, and it looks like the compiler
removes the references that aren't actually used by your code - i.e.
commenting out blocks of code can reduce the
assembly.GetReferencedAssembles() count... As such, I imagine that
adding some more code might make it start working. I am, however,
confused as to why you didn't see "System"... using copy/paste on your
code (in 2.0) gives me both System and mscorlib....

Marc
Edgile - 08 Jul 2007 08:04 GMT
Just to increase confusion:

static void Main(string[] args)
{
    Assembly assembly = Assembly.GetEntryAssembly();
    AssemblyName[] refs = assembly.GetReferencedAssemblies();
    Console.WriteLine(refs.Length);
    Type t = Type.GetType("ClassLibrary1.Class1, ClassLibrary1");
    object o = Activator.CreateInstance(t);
    refs = assembly.GetReferencedAssemblies();
    Console.WriteLine(refs.Length);
}

The type and the instance retrieved perfectly, while ClassLibrary1 still not
visible among referenced assemblies. It is not "dynamically referenced", it
just behaves odd hiding ClassLibrary1.

Is there any other suggestion, how to preload assemblies? My current
"solution" is browsing the files of the execution folder. It works fine as
long as I have read permission on the disk (as I have no interest in the
GAC), but on a web server having disk read permission is not an guarantee.

> > the resulted AssemblyName array contains one single element
>
[quoted text clipped - 7 lines]
>
> Marc
Marc Gravell - 08 Jul 2007 08:38 GMT
> but on a web server having disk read permission is not an guarantee

Inside the web-apps own scope, I would imagine that you'd have read
(but not write) access, otherwise the system is going to struggle;
especially if you just drop them in the "bin" folder. But you could
designate a "plugin" directory to which access is granted?
Edgile - 08 Jul 2007 08:16 GMT
>I am, however,
> confused as to why you didn't see "System"... using copy/paste on your
> code (in 2.0) gives me both System and mscorlib....

I believe the same reason you wrote above. They were not in use. I have no
clue why you have seen them (both 2.0), I have repeated the test on two
separate machines. They were consistent, only mscorlib was visible.
However, my personal interest is in my plugin system. I want to browse the
plugins via my plugin handler, both are referenced by a neutral host app. As
binding is prescribed in config files loaded during runtime, it has a fair
chance that types from the plugins will be used completely dynamically
without a single line of code referring to them.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.