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 / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Assembly.Load from byte[]

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
buttslapper@gmail.com - 17 Jul 2006 18:16 GMT
Hi,

Is it possible to load an assembly from a byte[] and make the framework
use this assembly instead of trying to locate them on disk..

Example :
My projet has a reference to the ESI.Framework assembly.

Assembly.Load(EmbeddedData.ESI.Framework);
ESI.Framework.Console c = new ESI.Framework.Console();

How can I bind the explicit reference that I added to my projet, to the
assembly loaded from my byte[]...
David Browne - 17 Jul 2006 18:55 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> How can I bind the explicit reference that I added to my projet, to the
> assembly loaded from my byte[]...

Yes.  You can add a custom AssemblyResolveEventHandler for your AppDomain.
If .NET can't find a referenced assembly in the normal places, it will call
any registered AssemblyResolveEventHandlers and give them the opportunity to
find and load the target assembly.

The only thing to be careful of is that you must add the ResolveEventHandler
before the target assembly is needed.  In practice this means that the class
that holds your Main method cannot directly invoke any type in the target
assembly.  However you Main method can call another type in the same
assembly which references the target assembly.

EG

   class Program
   {
       static void Main(string[] args)
       {
           AppDomain myApp = AppDomain.CurrentDomain;
           myApp.AssemblyResolve += new ResolveEventHandler(LoadMyLib);

           Startup.Run();  //Startup can use MyLib

       }

       static System.Reflection.Assembly LoadMyLib(object sender,
ResolveEventArgs args)
       {
           Console.WriteLine(args.Name);
           if (args.Name == "MyLibName, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null")
           {
               byte[] assemblyBytes =
System.IO.File.ReadAllBytes(@"C:\whatever\MyLibName.dll");
               return System.Reflection.Assembly.Load(assemblyBytes);
           }
           else
           {
               return null;
           }
       }
   }

David
buttslapper@gmail.com - 17 Jul 2006 19:19 GMT

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.