AssemblyPart.Load Method
Silverlight
Namespace: System.Windows
Assembly: System.Windows (in System.Windows.dll)
Parameters
- assemblyStream
- Type: System.IO.Stream
The Stream to load into the current application domain.
Return Value
Type: System.Reflection.AssemblyThe Assembly that is subsequently loaded into the current application domain.
The following code example demonstrates how to use this method to download a remote assembly and then load it into the application domain.
<UserControl x:Class="SilverlightApplication.HomePage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://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>
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); } } }
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Inlining Prevention
The method that uses the class downloaded in the assembly needs to be marked with the No Inlining attribute in order to preven the jitter from inlining the method. The Jit compiler reserves this right, which makes the code sample listed here incorrect and buggy.
The attribute needs to be:
[MethodImpl[MethodImplOptions.NoInlining)]
The attribute needs to be:
[MethodImpl[MethodImplOptions.NoInlining)]
- 3/4/2011
- P. Monster