Programmatically find CLR version for a .NET assembly

When it comes to extracting information from an assembly, Red Gate’s Reflector or the .NET Framework ILDASM tool are two obvious choices.

However if you have to programmatically determine the CLR version of an assembly it was compiled against, then use the Assembly.ImageRuntimeVersion property as shown below

Note: Add a namespace reference to System.Reflection

static void Main(string[] args)
Assembly asmbly = null;
asmbly = Assembly.ReflectionOnlyLoadFrom(@"C:/FckEdit/FredCK.FCKeditorV1.dll");
Console.WriteLine("CLR version of 1st Assembly " + asmbly.ImageRuntimeVersion);
asmbly = Assembly.ReflectionOnlyLoadFrom(@"C:/FckEdit/FredCK.FCKeditorV2.dll");
Console.WriteLine("CLR version of 2nd Assembly " + asmbly.ImageRuntimeVersion);

As you can see, we use ReflectionOnlyLoadFrom to load an assembly into reflection only context and then use the ImageRuntimeVersion property to get the version of the CLR saved in the assembly’s manifest.



Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: