Silverlight 控件开发记录之 "extern alias” 关键字

早在.net2.0, 微软就发布了"extern alias" C#关键字,目的就是为了解决在参照不同的Assembly类型全名相同的问题

以前还没觉得有多大的用处,但在做Silverlight DesignTime开发时,深刻感觉到它的必要了。原因很简单,SilverlightDesignTimeIDE/Blend)都是WPF程序,有很多类型都是同时存在于SLWPF assembly 中,比如System.Windows.FrameworkElement 。所以我们需要用这个关键字来明确指出代码中的类型到底属于SL或者WPF

System.Windows.FrameworkElement 就在PresentationFramework.dll(WPF)System.Windows.dll(SL)中都有定义,当我们在DesignTime代码里需要使用FrameworkElement 时,可以如此调用:

 

代码
 1 // sl 就是System.Windows.dll的别名,在IDE里你可以用PropertyWindow去定义此Assembly的Aliases属性
 2 //
 3 extern alias sl;
 4 
 5 using System;
 6 using System.Windows;
 7 internal class SampleDesignTimeClass
 8 {
 9 private void SampleMethod(Type myControlType)
10 {
11 // 现在使用的FrameworkElement就是定义在SL中的Type了
12 //
13 if (typeof(sl::System.Windows.FrameworkElement).IsAssignableFrom(myControlType))
14 {
15 ……
16 }
17 }
18  
19 }
20 

 

 

posted @ 2010-07-13 15:58  Alen在西安  阅读(220)  评论(0编辑  收藏  举报