views:

836

answers:

2

I want to show my Silverlight 3 application's version number in the about box, but when I use a traditional .Net call like:

Assembly.GetExecutingAssembly().GetName().Version;

I get a MethodAccessException on the GetName() call. How am I supposed to get the version number of my assembly?

+8  A: 
private static Version ParseVersionNumber(Assembly assembly)
        {
            AssemblyName assemblyName = new AssemblyName(assembly.FullName);
            return assemblyName.Version;
        }

or this:

Assembly assembly = Assembly.GetExecutingAssembly(); 
String version = assembly.FullName.Split(',')[1];
String fullversion = version.Split('=')[1]; 

From: http://betaforums.silverlight.net/forums/p/128861/288595.aspx

a post about it:

http://forums.silverlight.net/forums/p/93400/214554.aspx

You can look at the js file I posted here: http://stackoverflow.com/questions/1862801/detect-silverlight-version-required-by-an-assembly/2219515#2219515

Your error is expected.as it is secutiry critical, above are some work arounds.

James Campbell
GetExecutingAssembly also returns an Assembly type, its the call the GetName which fails.
AnthonyWJones
that should get you through it, I just tested it and it works fine.
James Campbell
I like the first solution. It seems cleaner. Both do work, however.
Dov
+1 for the first excellent solution, although I'd return an assembly name and let the caller access the `Version` property. The rest however is just awkard link heavy fluff, this answer would be way better and cleaner with just the simple first approach. @Dov, this is IMO your answer.
AnthonyWJones
+2  A: 

GetName is marked as Security Critical and hence you get an exception when you attempt to call it.

You will need to use the FullName property and parse out the Version=x.x.x.x part of the string.

AnthonyWJones
they are really paranoid the microsoft guys aren't they
herzmeister der welten
@herzmeister der welten: It would only take one minor hiccup in the Silverlight sandbox to do some serious damage to its reputation. At this stage in the life of Silverlight Microsoft cannot afford any such hiccup. I suspect that there are many things that don't actually represent a threat but because they aren't vital and haven't had thorough security anaylsis and testing they'll have the Security Critical attribute just to be safe.
AnthonyWJones