Even a chimp can write code

Sunday, June 08, 2008

How to tell a Silverlight assembly from a .NET Framework one?

Silverlight 2 application code is compiled to Common IL and packaged into assemblies (.dll) which are in turn deflated into a Zip file (container with .xap extension). The IL is a CPU- and platform- independent instruction set. The Silverlight runtime has in it the Core CLR which executes this IL. Given that .NET Framework involves much of the same things save for the .xap packaging, how does one tell between a DLL built for Silverlight versus one for .NET Framework?

Well, the answer is in one of the security assumptions that Silverlight's Core CLR makes to distinguish a core platform assembly from a transparent code app assembly. The mscorlib.dll in Silverlight is signed with a different key than it's namesake in .NET Framework. This is apparent in it's full name, or specifically it's public key token.

The public key token for mscorlib in Silverlight is:

7cec85d7bea7798e

while the public key token for mscorlib in .NET Framework is:

b77a5c561934e089

So the trick to sniffing out an arbitrary managed code assembly for its allegiance - Silverlight or .NET Framework - is to look through it's list of referenced assemblies, locate mscorlib and then check the public key token. Here is a sample application that does that.

Labels: ,

Email this | Bookmark this

5 Comments:

  • Ashish, since most controls have been moved to plugin, what do I need to do to my project, CS and Xaml files to make my XAP file smaller than Beta 1?

    I've removed unused USING statements from my CS files, but my XAP is still the same size as Beta 1.

    Thanks!
    ..Ben

    By Anonymous Anonymous, at June 8, 2008 at 2:12 PM  

  • I'm just guessing: did you remove the dll files copied across when the using statement has been written? As far as I know, all dlls in the folder are going to be packed so since you don't need some of then, simply delete them.

    By Anonymous Anonymous, at June 8, 2008 at 5:50 PM  

  • Make sure the References in your project accurately reflect the assemblies you're relying on. A minimalist Silverlight app would only require mscorlib.dll, System.dll and System.Windows.dll. If you remove the extensions to the platform such as System.Windows.Controls.Extended.dll from your References, then you'll notice size savings in the xap. If your xap happens to use base as well as extended controls, well then you won't see much savings.

    By Blogger Ashish Shetty, at June 8, 2008 at 6:18 PM  

  • Thanks to Chix and Ashish for both great points.

    Chix, your point is well taken. I didn't even think about that.

    By Anonymous Anonymous, at June 8, 2008 at 7:46 PM  

  • Great thinking.

    By Blogger Unknown, at June 11, 2008 at 7:27 AM  

Post a Comment | Home | Inference: my personal blog