using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: Foo.Bar.AsyncEnumerableWrapperFactory<Foo.Bar.Test1Factory>]
public static void Main()
var domain = AppDomain.CurrentDomain;
var files = new List<string>();
files.AddRange(Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll"));
files.AddRange(Directory.GetFiles(domain.BaseDirectory, "*.dll"));
files.AddRange(Directory.GetFiles(Path.Combine(domain.BaseDirectory, domain.RelativeSearchPath ?? string.Empty), "*.dll"));
Console.WriteLine(typeof(MetadataLoadContext).Assembly.Location);
using var context = new MetadataLoadContext(new PathAssemblyResolver(files));
context.LoadFromAssemblyPath(Assembly.GetEntryAssembly().Location);
context.LoadFromAssemblyPath(Assembly.GetCallingAssembly().Location);
context.LoadFromAssemblyPath(Assembly.GetExecutingAssembly().Location);
var allAssemblies = new HashSet<Assembly>();
foreach (var assembly in context.GetAssemblies())
Console.WriteLine(assembly.FullName);
var asm = context.LoadFromAssemblyPath(assembly.Location);
foreach (var referencedAssembly in asm.GetReferencedAssemblies())
allAssemblies.Add(context.LoadFromAssemblyName(referencedAssembly));
Console.WriteLine($"\t{referencedAssembly.FullName}");
Console.WriteLine($"failed referenced assembly: {referencedAssembly.FullName}");
var targetAssemblyName = allAssemblies
.First(x => x.GetCustomAttributesData().Any(x => x.AttributeType.FullName.StartsWith(typeof(AsyncEnumerableWrapperFactoryAttribute<>).FullName)))
.Load(targetAssemblyName)
.GetCustomAttribute(typeof(AsyncEnumerableWrapperFactoryAttribute<>))
as IAsyncEnumerableWrapperFactory;
var foo = new[] { "a", "b", "c" }.AsQueryable();
Console.WriteLine(nameof(Main));
var a = new StackTrace(false).GetFrames().Select(Foo).ToArray();
public static bool Foo(StackFrame frame)
var method = frame.GetMethod();
var assembly = method.DeclaringType.Assembly.FullName;
Console.WriteLine($"{name} : {assembly}");
[AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
public sealed class AsyncEnumerableWrapperFactoryAttribute<TFactory> : Attribute, IAsyncEnumerableWrapperFactory
where TFactory : IAsyncEnumerableWrapperFactory, new()
public TFactory Factory { get; } = new();
public IAsyncEnumerable<TSource> Create<TSource>(
IQueryable<TSource> source)
=> Factory.Create(source);
public interface IAsyncEnumerableWrapperFactory
public IAsyncEnumerable<TSource> Create<TSource>(IQueryable<TSource> source) where TSource : class;
public sealed class Test1Factory : IAsyncEnumerableWrapperFactory
public IAsyncEnumerable<TSource> Create<TSource>(IQueryable<TSource> source) where TSource : class
Console.WriteLine(nameof(Test1Factory));