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);
Console.ReadLine();
}


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.

OUTPUT

image






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: