IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
VfsRuntimeContext.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5
6#if NETCOREAPP3_1_OR_GREATER
7using System.Runtime.Loader;
8
9using Microsoft.Extensions.DependencyModel;
10#endif
11
12namespace IKVM.Runtime.Vfs
13{
14
18 internal class VfsRuntimeContext : VfsContext
19 {
20
21 readonly RuntimeContext context;
22
27 public VfsRuntimeContext(RuntimeContext context)
28 {
29 this.context = context ?? throw new ArgumentNullException(nameof(context));
30 }
31
35 public override RuntimeContext Context => context;
36
41 public override Assembly GetAssembly(AssemblyName name)
42 {
43 try
44 {
45#if NETFRAMEWORK
46 return AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(i => IsMatch(i.GetName(), name)) ?? Assembly.Load(name);
47#else
48 return AssemblyLoadContext.GetLoadContext(typeof(VfsRuntimeContext).Assembly).LoadFromAssemblyName(name);
49#endif
50 }
51 catch
52 {
53 return null;
54 }
55 }
56
61 public override IEnumerable<AssemblyName> GetAssemblyNames()
62 {
63#if NETFRAMEWORK
64 return AppDomain.CurrentDomain.GetAssemblies().Select(i => i.GetName()).Distinct();
65#else
66 return AssemblyLoadContext.GetLoadContext(typeof(VfsRuntimeContext).Assembly).Assemblies.Select(i => i.GetName());
67#endif
68 }
69
76 bool IsMatch(AssemblyName reference, AssemblyName definition)
77 {
78 // match full name
79 if (definition.Name != null && string.Equals(definition.Name, reference.Name, StringComparison.OrdinalIgnoreCase) == false)
80 return false;
81
82 // match version
83 if (definition.Version != null && definition.Version != reference.Version)
84 return false;
85
86 // match culture
87 if (definition.CultureName != null && string.Equals(definition.CultureName, reference.CultureName, StringComparison.OrdinalIgnoreCase) == false)
88 return false;
89
90 // match public key
91 if (definition.GetPublicKeyToken() != null && definition.GetPublicKeyToken().AsSpan().SequenceEqual(reference.GetPublicKeyToken()) == false)
92 return false;
93
94 return true;
95 }
96
97 }
98
99}
IKVM.Reflection.Assembly Assembly
IKVM.Reflection.AssemblyName AssemblyName