//FIXME: the fallback is broken since multiple frameworks can have the same corlib public TargetFrameworkMoniker GetTargetFrameworkForAssembly(TargetRuntime tr, string file) { if (!File.Exists(file)) { return(TargetFrameworkMoniker.UNKNOWN); } var universe = CreateClosedUniverse(); try { IKVM.Reflection.Assembly assembly = universe.LoadFile(file); var att = assembly.CustomAttributes.FirstOrDefault(a => a.AttributeType.FullName == "System.Runtime.Versioning.TargetFrameworkAttribute" ); if (att != null) { if (att.ConstructorArguments.Count == 1) { var v = att.ConstructorArguments[0].Value as string; TargetFrameworkMoniker m; if (v != null && TargetFrameworkMoniker.TryParse(v, out m)) { return(m); } } LoggingService.LogError("Invalid TargetFrameworkAttribute in assembly {0}", file); } foreach (var r in assembly.GetReferencedAssemblies()) { if (r.Name == "mscorlib") { TargetFramework compatibleFramework = null; // If there are several frameworks that can run the file, pick one that is installed foreach (TargetFramework tf in GetKnownFrameworks()) { if (tf.GetCorlibVersion() == r.Version.ToString()) { compatibleFramework = tf; if (tr.IsInstalled(tf)) { return(tf.Id); } } } if (compatibleFramework != null) { return(compatibleFramework.Id); } break; } } } catch (Exception ex) { LoggingService.LogError("Error determining target framework for assembly {0}: {1}", file, ex); return(TargetFrameworkMoniker.UNKNOWN); } finally { universe.Dispose(); } LoggingService.LogError("Failed to determine target framework for assembly {0}", file); return(TargetFrameworkMoniker.UNKNOWN); }
private static System.Reflection.Assembly LoadPlugin(IKVM.Reflection.Assembly asm) { foreach (var name in asm.GetManifestResourceNames()) { if (name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { using (var strm = asm.GetManifestResourceStream(name)) using (var ms = new MemoryStream()) using (var uni = new IKVM.Reflection.Universe()) { strm.CopyTo(ms); ms.Position = 0; string referenceName = uni.LoadAssembly(uni.OpenRawModule(ms, name)).GetName().Name; var result = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == referenceName); if (result == null) { result = System.Reflection.Assembly.Load(ms.ToArray()); } return(result); } } } return(null); }
private static IEnumerable <string> GetReferencedAssemblyNames(IKVM.Reflection.Assembly asm) { return(asm.GetReferencedAssemblies().Select(r => r.Name)); }