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 files = new List<string>(Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll"));
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: {assembly.FullName}");
var asm = context.LoadFromAssemblyPath(assembly.Location);
foreach (var referencedAssembly in asm.GetReferencedAssemblies())
Console.WriteLine($"referenced assembly: {referencedAssembly.FullName}");
allAssemblies.Add(context.LoadFromAssemblyName(referencedAssembly));
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));