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

No comments:

Post a Comment