首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

动态加载程序集有关问题

2012-02-23 
动态加载程序集问题在SL中,可以通过WebClient加载某个DLL,或ZIP、XAP等,然后通过AssemblyPart的Load方法加

动态加载程序集问题
在SL中,可以通过WebClient加载某个DLL,或ZIP、XAP等,然后通过AssemblyPart的Load方法加载程序集
问题是,假设某个dll已经被成功加载,那么下次应用的时候,我就可以不必再通过WebClient加载这个dll了
所以我需要一个判断,就是判断某个程序集是否已经被加载,如果已经加载过,就不再加载。如何实现这个判断?

例如,我有一个类,DemoCtrl。在没加载它的程序集之前,DemoCtrl dc = new DemoCtrl();这样的代码是出错的
所以我必须先通过WebClient加载DLL,再通过AssemblyPart来Load它,然后就可以执行了
所以,要判断DemoCtrl这个类是否已经存在

[解决办法]
需要构件一个全局的类或者集合来保存程序集加载的信息
在IsLoad中查找全局集合来判断是否已加载过程序集
[解决办法]
SL3.0以上版本尝试使用Assembly.GetAssembly方法参考:

C# code
Assembly SampleAssembly;// Instantiate a target object.Int32 Integer1 = new Int32();Type Type1;// Set the Type instance to the target class type.Type1 = Integer1.GetType();// Instantiate an Assembly class to the assembly housing the Integer type.  SampleAssembly = Assembly.GetAssembly(Integer1.GetType());// Display the name of the assembly currently executingConsole.WriteLine("GetExecutingAssembly=" + Assembly.GetExecutingAssembly().FullName);
[解决办法]
肯定有的
System.Reflection.Assembly.GetExecutingAssembly();
[解决办法]
可以建立assembly的实例,然后对实例进行判断就可以了。
例如

C# code
private void loadAssembly(Stream s){     try     {          AssemblyPart ap = new AssemblyPart();          Assembly a = ap.Load(s);          Canvas can = (Canvas)a.CreateInstance("MyAssembly.MyClass");          if (can == null)          {                 debug("can is null!!!!!!!!!");                 return;          }          this.Children.Add(can);          debug("Canvas successfully added");     }     catch (Exception ex)     {          debug("Exception when loading the assembly part:");          debug(ex.ToString());     }}
[解决办法]
还是用一个全局变量来判断好些。
[解决办法]
尝试使用prism
http://www.cnblogs.com/jax/archive/2009/05/25/1488797.html

热点排行