26

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?

starblue
  • 55,348
  • 14
  • 97
  • 151
Dov
  • 15,530
  • 13
  • 76
  • 177

3 Answers3

48
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: Detect Silverlight version required by an assembly

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

Community
  • 1
  • 1
James Campbell
  • 5,057
  • 2
  • 35
  • 54
  • 2
    GetExecutingAssembly also returns an Assembly type, its the call the GetName which fails. – AnthonyWJones Feb 19 '10 at 15:16
  • that should get you through it, I just tested it and it works fine. – James Campbell Feb 19 '10 at 15:20
  • I like the first solution. It seems cleaner. Both do work, however. – Dov Feb 19 '10 at 15:30
  • +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 Feb 19 '10 at 15:42
  • Great solution. The crazy thing is that assembly.GetCustomAttributes doesn't return the AssemblyVersion attribute -- it returns most of the others that I expected (company/trademark/etc) – JMarsch Mar 27 '12 at 21:49
6

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
  • 187,081
  • 35
  • 232
  • 306
  • they are really paranoid the microsoft guys aren't they – herzmeister Feb 19 '10 at 15:17
  • 2
    @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 Feb 19 '10 at 15:34
1

You can use

Assembly.GetExecutingAssembly()
 .GetCustomAttributes(false).OfType<AssemblyVersionAttribute>()
 .Single().Version;
gius
  • 9,289
  • 3
  • 33
  • 62
  • GetCustomAttributes(bool) will not return AssemblyVersionAttribute, because it is treated in a "special" way. You can still use AssemblyFileVersionAttribute and it will work. Otherwise, thanks for the answer that does not include parsing strings :) – noaRAVE Aug 29 '14 at 19:01