Silverlight中的反射 .

 

Silverlight中的反射与.NET FRAMEWORK中的略有不同。以下分三种情况描述:

     1:动态创建当前执行的程序集中的类实例。

     2:动态创建XAP包中其它SILVERLIGHT程序集中的类实例。

     3:动态下载并创建网站上其它SILVERLIGHT程序集中的类实例。

 

1:动态创建当前执行的程序集中的类实例

     示例代码如下,其中ChildWindowMsg是你自己创建的类。

  1. Assembly assem = Assembly.GetExecutingAssembly();  
  2. ChildWindowMsg cw = assem.CreateInstance("YiPin.QuestionsDbSL.ChildWindowMsg"as ChildWindowMsg;  

 

2:动态创建XAP包中其它SILVERLIGHT程序集中的类实例

  1. Assembly assem = Assembly.GetExecutingAssembly();  
  2. ChildWindowMsg cw = Assembly.Load("YiPin.QuestionsDbSL").CreateInstance("YiPin.QuestionsDbSL.ChildWindowMsg"as ChildWindowMsg;  
  3.               

 

3:动态下载并创建网站上其它SILVERLIGHT程序集中的类实例

  1. For example we can write a usercontrol to download the assembly:  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Animation;  
  12. using System.Windows.Shapes;  
  13. using System.Reflection;  
  14. namespace SilverlightApplication15  
  15. {  
  16.       
  17.     public partial class DownloadUserControl : UserControl  
  18.     {  
  19.         public string Source { getset; }  
  20.         public DownloadUserControl()  
  21.         {  
  22.             InitializeComponent();  
  23.             this.Loaded += new RoutedEventHandler(DownloadUserControl_Loaded);  
  24.            
  25.         }  
  26.         void DownloadUserControl_Loaded(object sender, RoutedEventArgs e)  
  27.         {  
  28.             WebClient client = new WebClient();  
  29.             client.OpenReadCompleted += new OpenReadCompletedEventHandler(OnAssemblyOpened);  
  30.             client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);  
  31.             client.OpenReadAsync(new Uri(Source));  
  32.         }  
  33.         void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)  
  34.         {  
  35.             this.txtProgress.Text = e.ProgressPercentage + "%";  
  36.         }  
  37.         void OnAssemblyOpened(object sender, OpenReadCompletedEventArgs e)  
  38.         {  
  39.              
  40.                 AssemblyPart assemblyPart = new AssemblyPart();  
  41.                 Assembly assembly = assemblyPart.Load(e.Result);  
  42.                 Control largecontrol = assembly.CreateInstance("LargeCustomControl.LargeControl"as Control;  
  43.                 if (largecontrol != null) {  
  44.                     this.txtProgress.Visibility = Visibility.Collapsed;  
  45.                     this.LayoutRoot.Children.Add(largecontrol);  
  46.                 }  
  47.              
  48.              
  49.         }  
  50.     }  
  51. }  
  52. <UserControl x:Class="SilverlightApplication15.DownloadUserControl"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  53.     Width="400" Height="300">  
  54.     <Grid x:Name="LayoutRoot" Background="White">  
  55.         <TextBlock x:Name="txtProgress"></TextBlock>  
  56.     </Grid>  
  57. </UserControl>  
  58.   
  59.   To use it in Page.xaml:  
  60.   <c:DownloadUserControl Source="http://localhost/LargeCustomControl.dll"></c:DownloadUserControl>  
  61.   
  62.   You just have to privde the uri of the dll of your large custom control.  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值