Get assembly version in PCL

kls picture kls · May 13, 2013 · Viewed 7.1k times · Source

I have the following line of code in .NET 4.5 that I am trying to build as Portable Class Library. It's purpose is to get assembly version:

this.GetType().Assembly.GetName().Version.Major;

The problem is that Assembly.GetName() is not available in PCL. Is there a way to get assembly version in PCL?

I know it is possible to parse Assembly.FullName, but I want a better solution.

Answer

pauldendulk picture pauldendulk · May 13, 2013
    public static string Version
    {
        get
        {
            var assembly = typeof(MyType).GetTypeInfo().Assembly;
            // In some PCL profiles the above line is: var assembly = typeof(MyType).Assembly;
            var assemblyName = new AssemblyName(assembly.FullName);
            return assemblyName.Version.Major + "." + assemblyName.Version.Minor;
        }
    }