Friday, October 03, 2008

I’ll slowly try to start blogging again :)  It’s been extremely hectic, and I am slowly ramping up. Lately I’ve not been blogging or been just scribbling non-technical jibberish here. Let’s get back on the train!

Loading assemblies in Silverlight can be a bit hard because everything is packaged up in a Xap file. If you want to get to all the assemblies and start instantiating types, you’ll quickly run into a wall.

However, Windows is about living without walls, and thus there is a workaround! :)

  0    string assemblyname = "System.Windows.Controls";
  1
  2    AssemblyPart part = Deployment.Current.Parts.SingleOrDefault(asmpart => asmpart.GetValue(FrameworkElement.NameProperty).ToString() == assemblyname);
  3
  4    StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri(part.Source, UriKind.Relative));
  5    Assembly asm = part.Load(streamInfo.Stream);
  6

It appears there is a very nice class available: Deployment. It is a list of the deployment section of your App.Manifest. The manifest describes all the ‘DeploymentParts’ in the Xap file. It thus also describes the filenames of the dll’s that are included.

If you know the dll name, that is great, and you can easily load it; skip to line 4.
In my case, I just had the assembly name (which does not mean I have the dll name) and on line 2 you can see me running through the parts that are available to me. The x:Name in the deployment file is the name of my assembly, so I get to it by using the DependencyProperty system: querying for the FrameworkElement.NameProperty.

Once I have an assembly part, I have the actual filename: part.Source. So, on line 4, I can now setup a stream to it, and on line 5 I can easily load my assembly.

From this point on, I can do everything you would expect.

Happy sailing!

Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):