Partilhar via


How to: Load Assemblies On Demand

Microsoft Silverlight will reach end of support after October 2021. Learn more.

The following example code demonstrates how to retrieve an assembly from the host server on demand, and then load it into the current application domain.

This example uses the WebClient class to initiate an asynchronous download of the assembly in response to a user mouse click. When the download is complete, the AssemblyPart class is used to load the assembly.

This example assumes that you have added a reference to the assembly in your application project. The assembly must also have a Copy Local value of False so that it will not be deployed in the application package. For more information, see Application Structure.

You can use this technique to load assemblies that are not referenced in your application project. In this case, however, you must use reflection to instantiate types from the loaded assembly. For more information, see Assembly.

Example

The following example loads an assembly called SilverlightLibrary.dll when the user clicks a text block. This example uses a relative URI to load the assembly from the same location as the application XAP file.

<UserControl x:Class="SilverlightApplication.HomePage"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Background="SandyBrown">
        <StackPanel x:Name="stackPanel">
            <TextBlock>Page from Host Application</TextBlock>
            <TextBlock 
                MouseLeftButtonUp="TextBlock_MouseLeftButtonUp"
                Cursor="Hand">
                Click Here to Display a UI from the Library Assembly
            </TextBlock>
        </StackPanel>
    </Grid>


</UserControl>
Imports SilverlightLibrary

Partial Public Class HomePage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub TextBlock_MouseLeftButtonUp(ByVal sender As Object, _
        ByVal e As MouseButtonEventArgs)

        ' Download an "on-demand" assembly. 
        Dim wc As New WebClient()
        AddHandler wc.OpenReadCompleted, AddressOf wc_OpenReadCompleted
        wc.OpenReadAsync(New Uri("SilverlightLibrary.dll", UriKind.Relative))

    End Sub

    Private Sub wc_OpenReadCompleted(ByVal sender As Object, _
        ByVal e As OpenReadCompletedEventArgs)

        If (e.[Error] Is Nothing) AndAlso (e.Cancelled = False) Then

            ' Convert the downloaded stream into an assembly that is 
            ' loaded into current AppDomain. 
            Dim assemblyPart As New AssemblyPart()
            assemblyPart.Load(e.Result)

            DisplayPageFromLibraryAssembly()

        End If

    End Sub

    Private Sub DisplayPageFromLibraryAssembly()

        ' Create an instance of the Page class in the library assembly. 
        Dim page As New Page()

        ' Display the new Page. 
        Me.stackPanel.Children.Add(page)

    End Sub

End Class
using System; // Uri
using System.Net; // WebClient,OpenReadCompletedEventHandler
using System.Windows; // AssemblyPart
using System.Windows.Controls; // UserControl
using System.Windows.Input; // MouseButtonEventArgs
using SilverlightLibrary; // Page

namespace SilverlightApplication
{
    public partial class HomePage : UserControl
    {
        public HomePage()
        {
            InitializeComponent();
        }

        private void TextBlock_MouseLeftButtonUp(
            object sender, MouseButtonEventArgs e)
        {
            // Download an "on-demand" assembly.
            WebClient wc = new WebClient();
            wc.OpenReadCompleted += 
                new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(
                new Uri("SilverlightLibrary.dll", UriKind.Relative));
        }

        private void wc_OpenReadCompleted(
            object sender, OpenReadCompletedEventArgs e)
        {
            if ((e.Error == null) && (e.Cancelled == false))
            {
                // Convert the downloaded stream into an assembly that is
                // loaded into current AppDomain.
                AssemblyPart assemblyPart = new AssemblyPart();
                assemblyPart.Load(e.Result);

                DisplayPageFromLibraryAssembly();
            }
        }

        private void DisplayPageFromLibraryAssembly() {

            // Create an instance of the Page class in the library assembly.
            Page page = new Page();

            // Display the new Page. 
            this.stackPanel.Children.Add(page);
        }
    }
}

To view a running version of this example, click the following link.

 

Run this sample