|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using Xunit.Internal; |
| 8 | +using Xunit.Sdk; |
| 9 | + |
| 10 | +namespace Xunit.Runner.VisualStudio; |
| 11 | + |
| 12 | +class DiaSessionWrapperHelper : LongLivedMarshalByRefObject |
| 13 | +{ |
| 14 | + readonly Assembly? assembly; |
| 15 | + readonly Dictionary<string, Type> typeNameMap; |
| 16 | + |
| 17 | + public DiaSessionWrapperHelper(string assemblyFileName) |
| 18 | + { |
| 19 | + try |
| 20 | + { |
| 21 | +#if NETFRAMEWORK |
| 22 | + assembly = Assembly.ReflectionOnlyLoadFrom(assemblyFileName); |
| 23 | + var assemblyDirectory = Path.GetDirectoryName(assemblyFileName); |
| 24 | + |
| 25 | + if (assemblyDirectory is not null) |
| 26 | + AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += (sender, args) => |
| 27 | + { |
| 28 | + try |
| 29 | + { |
| 30 | + // Try to load it normally |
| 31 | + var name = AppDomain.CurrentDomain.ApplyPolicy(args.Name); |
| 32 | + return Assembly.ReflectionOnlyLoad(name); |
| 33 | + } |
| 34 | + catch |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + // If a normal implicit load fails, try to load it from the directory that |
| 39 | + // the test assembly lives in |
| 40 | + return Assembly.ReflectionOnlyLoadFrom( |
| 41 | + Path.Combine( |
| 42 | + assemblyDirectory, |
| 43 | + new AssemblyName(args.Name).Name + ".dll" |
| 44 | + ) |
| 45 | + ); |
| 46 | + } |
| 47 | + catch |
| 48 | + { |
| 49 | + // If all else fails, say we couldn't find it |
| 50 | + return null; |
| 51 | + } |
| 52 | + } |
| 53 | + }; |
| 54 | +#else |
| 55 | + assembly = Assembly.Load(new AssemblyName { Name = Path.GetFileNameWithoutExtension(assemblyFileName) }); |
| 56 | +#endif |
| 57 | + } |
| 58 | + catch { } |
| 59 | + |
| 60 | + if (assembly is not null) |
| 61 | + { |
| 62 | + Type?[]? types = null; |
| 63 | + |
| 64 | + try |
| 65 | + { |
| 66 | + types = assembly.GetTypes(); |
| 67 | + } |
| 68 | + catch (ReflectionTypeLoadException ex) |
| 69 | + { |
| 70 | + types = ex.Types; |
| 71 | + } |
| 72 | + catch { } // Ignore anything other than ReflectionTypeLoadException |
| 73 | + |
| 74 | + if (types is not null) |
| 75 | + typeNameMap = |
| 76 | + types |
| 77 | + .WhereNotNull() |
| 78 | + .Where(t => !string.IsNullOrEmpty(t.FullName)) |
| 79 | + .ToDictionaryIgnoringDuplicateKeys(k => k.FullName!); |
| 80 | + } |
| 81 | + |
| 82 | + typeNameMap ??= []; |
| 83 | + } |
| 84 | + |
| 85 | + public void Normalize( |
| 86 | + ref string typeName, |
| 87 | + ref string methodName) |
| 88 | + { |
| 89 | + try |
| 90 | + { |
| 91 | + if (assembly is null) |
| 92 | + return; |
| 93 | + |
| 94 | + if (typeNameMap.TryGetValue(typeName, out var type) && type is not null) |
| 95 | + { |
| 96 | + var method = type.GetMethod(methodName); |
| 97 | + if (method is not null && method.DeclaringType is not null && method.DeclaringType.FullName is not null) |
| 98 | + { |
| 99 | + // DiaSession only ever wants you to ask for the declaring type |
| 100 | + typeName = method.DeclaringType.FullName; |
| 101 | + |
| 102 | + // See if this is an async method by looking for [AsyncStateMachine] on the method, |
| 103 | + // which means we need to pass the state machine's "MoveNext" method. |
| 104 | + var stateMachineType = method.GetCustomAttribute<AsyncStateMachineAttribute>()?.StateMachineType; |
| 105 | + if (stateMachineType is not null && stateMachineType.FullName is not null) |
| 106 | + { |
| 107 | + typeName = stateMachineType.FullName; |
| 108 | + methodName = "MoveNext"; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + catch { } |
| 114 | + } |
| 115 | +} |
0 commit comments