MetaDataReader(string moduleName) { CLRMetaHost mh = new CLRMetaHost(); CLRRuntimeInfo highestInstalledRuntime = null; foreach (CLRRuntimeInfo runtime in mh.EnumerateInstalledRuntimes()) { if (highestInstalledRuntime == null || string.Compare(highestInstalledRuntime.GetVersionString(), runtime.GetVersionString(), StringComparison.OrdinalIgnoreCase) < 0) { highestInstalledRuntime = runtime; } } if (highestInstalledRuntime == null) { throw new ApplicationException("Could not enumerate .NET runtimes on the system."); } IMetaDataDispenser metaDataDispenser = highestInstalledRuntime.GetIMetaDataDispenser(); Guid IMetaDataImport2_Guid = Guid("7DAC8207-D3AE-4c75-9B67-92801A497D44"); object metaDataImportObj; metaDataDispenser.OpenScope(moduleName, 0, ref IMetaDataImport2_Guid, out metaDataImportObj); m_metaDataImport = metaDataImportObj as IMetadataImport2; }
/// <summary> /// Given the full path to a binary, finds the CLR runtime version which /// it will bind to. /// </summary> /// <param name="filePath">The full path to a binary.</param> /// <returns>The version string that the binary will bind to; null if unknown.</returns> /// <remarks>If ICLRMetaHostPolicy can be asked, it is used. Otherwise /// fall back to mscoree!GetRequestedRuntimeVersion.</remarks> public static String GetDefaultRuntimeForFile(String filePath) { String version = null; CLRMetaHostPolicy policy; try { policy = new CLRMetaHostPolicy(); } catch (NotImplementedException) { policy = null; } catch (System.EntryPointNotFoundException) { policy = null; } if (policy != null) { // v4 codepath StringBuilder ver = new StringBuilder(); StringBuilder imageVer = new StringBuilder(); CLRRuntimeInfo rti = null; String configPath = null; if (System.IO.File.Exists(filePath + ".config")) { configPath = filePath + ".config"; } try { rti = policy.GetRequestedRuntime(CLRMetaHostPolicy.MetaHostPolicyFlags.metaHostPolicyHighCompat, filePath, configPath, ref ver, ref imageVer); } catch (System.Runtime.InteropServices.COMException) { Debug.Assert(rti == null); } if (rti != null) { version = rti.GetVersionString(); } else { version = null; } } else { // v2 codepath try { version = CorDebugger.GetDebuggerVersionFromFile(filePath); } catch (System.Runtime.InteropServices.COMException) { // we could not retrieve dee version. // Leave version null; Debug.Assert(version == null); } } return(version); }