So my approach with this challenge meant diving through a whole load of reference source to understand the different types available to Roslyn.
To prefix the end solution, lets create the module interface, we'll put this in Contracts.dll
:
public interface IModule
{
public int Order { get; }
public string Name { get; }
public Version Version { get; }
IEnumerable<ServiceDescriptor> GetServices();
}
public interface IModuleProvider
{
IEnumerable<IModule> GetModules();
}
And let's also define out base provider:
public abstract class ModuleProviderBase
{
private readonly List<IModule> _modules = new List<IModule>();
protected ModuleProviderBase()
{
Setup();
}
public IEnumerable<IModule> GetModules()
{
return _modules.OrderBy(m => m.Order);
}
protected void AddModule<T>() where T : IModule, new()
{
var module = new T();
_modules.Add(module);
}
protected virtual void Setup() { }
}
Now, in this architecture, the module isn't really anything more than a descriptor, so shouldn't take dependencies, it merely expresses what services it offers.
Now an example module might look like, in DefaultLogger.dll
:
public class DefaultLoggerModule : ModuleBase
{
public override int Order { get { return ModuleOrder.Level3; } }
public override IEnumerable<ServiceDescriptor> GetServices()
{
yield return ServiceDescriptor.Instance<ILoggerFactory>(new DefaultLoggerFactory());
}
}
I've left out the implementation of ModuleBase
for brevity.
Now, in my web project, I add a reference to Contracts.dll
and DefaultLogger.dll
, and then add the following implementation of my module provider:
public partial class ModuleProvider : ModuleProviderBase { }
And now, my ICompileModule
:
using T = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree;
using F = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using K = Microsoft.CodeAnalysis.CSharp.SyntaxKind;
public class DiscoverModulesCompileModule : ICompileModule
{
private static MethodInfo GetMetadataMethodInfo = typeof(PortableExecutableReference)
.GetMethod("GetMetadata", BindingFlags.NonPublic | BindingFlags.Instance);
private static FieldInfo CachedSymbolsFieldInfo = typeof(AssemblyMetadata)
.GetField("CachedSymbols", BindingFlags.NonPublic | BindingFlags.Instance);
private ConcurrentDictionary<MetadataReference, string[]> _cache
= new ConcurrentDictionary<MetadataReference, string[]>();
public void AfterCompile(IAfterCompileContext context) { }
public void BeforeCompile(IBeforeCompileContext context)
{
// Firstly, I need to resolve the namespace of the ModuleProvider instance in this current compilation.
string ns = GetModuleProviderNamespace(context.Compilation.SyntaxTrees);
// Next, get all the available modules in assembly and compilation references.
var modules = GetAvailableModules(context.Compilation).ToList();
// Map them to a collection of statements
var statements = modules.Select(m => F.ParseStatement("AddModule<" + module + ">();")).ToList();
// Now, I'll create the dynamic implementation as a private class.
var cu = F.CompilationUnit()
.AddMembers(
F.NamespaceDeclaration(F.IdentifierName(ns))
.AddMembers(
F.ClassDeclaration("ModuleProvider")
.WithModifiers(F.TokenList(F.Token(K.PartialKeyword)))
.AddMembers(
F.MethodDeclaration(F.PredefinedType(F.Token(K.VoidKeyword)), "Setup")
.WithModifiers(
F.TokenList(
F.Token(K.ProtectedKeyword),
F.Token(K.OverrideKeyword)))
.WithBody(F.Block(statements))
)
)
)
.NormalizeWhitespace(indentation("\t"));
var tree = T.Create(cu);
context.Compilation = context.Compilation.AddSyntaxTrees(tree);
}
// Rest of implementation, described below
}
Essentially this module does a few steps;
1 - Resolves the namespace of the ModuleProvider
instance in the web project, e.g. SampleWeb
.
2 - Discovers all the available modules through references, these are returned as a collection of strings, e.g. new[] { "SampleLogger.DefaultLoggerModule" }
3 - Convert those to statements of the kind AddModule<SampleLogger.DefaultLoggerModule>();
4 - Create a partial
implementation of ModuleProvider
that we are adding to our compilation:
namespace SampleWeb
{
partial class ModuleProvider
{
protected override void Setup()
{
AddModule<SampleLogger.DefaultLoggerModule>();
}
}
}
So, how did I discover the available modules? There are three phases:
1 - The referenced assemblies (e.g., those provided through NuGet)
2 - The referenced compilations (e.g., the referenced projects in the solution).
3 - The module declarations in the current compilation.
And for each referenced compilation, we repeat the above.
private IEnumerable<string> GetAvailableModules(Compilation compilation)
{
var list = new List<string>();
string[] modules = null;
// Get the available references.
var refs = compilation.References.ToList();
// Get the assembly references.
var assemblies = refs.OfType<PortableExecutableReference>().ToList();
foreach (var assemblyRef in assemblies)
{
if (!_cache.TryGetValue(assemblyRef, out modules))
{
modules = GetAssemblyModules(assemblyRef);
_cache.AddOrUpdate(assemblyRef, modules, (k, v) => modules);
list.AddRange(modules);
}
else
{
// We've already included this assembly.
}
}
// Get the compilation references
var compilations = refs.OfType<CompilationReference>().ToList();
foreach (var compliationRef in compilations)
{
if (!_cache.TryGetValue(compilationRef, out modules))
{
modules = GetAvailableModules(compilationRef.Compilation).ToArray();
_cache.AddOrUpdate(compilationRef, modules, (k, v) => modules);
list.AddRange(modules);
}
else
{
// We've already included this compilation.
}
}
// Finally, deal with modules in the current compilation.
list.AddRange(GetModuleClassDeclarations(compilation));
return list;
}
So, to get assembly referenced modules:
private IEnumerable<string> GetAssemblyModules(PortableExecutableReference reference)
{
var metadata = GetMetadataMethodInfo.Invoke(reference, nul) as AssemblyMetadata;
if (metadata != null)
{
var assemblySymbol = ((IEnumerable<IAssemblySymbol>)CachedSymbolsFieldInfo.GetValue(metadata)).First();
// Only consider our assemblies? Sample*?
if (assemblySymbol.Name.StartsWith("Sample"))
{
var types = GetTypeSymbols(assemblySymbol.GlobalNamespace).Where(t => Filter(t));
return types.Select(t => GetFullMetadataName(t)).ToArray();
}
}
return Enumerable.Empty<string>();
}
We need to do a little reflection here as the GetMetadata
method is not public, and later, when we grab the metadata, the CachedSymbols
field is also non-public, so more reflection there. In terms of identifying what is available, we need to grab the IEnumerable<IAssemblySymbol>
from the CachedSymbols
property. This gives us all the cached symbols in the reference assembly. Roslyn does this for us, so we can then abuse it:
private IEnumerable<ITypeSymbol> GetTypeSymbols(INamespaceSymbol ns)
{
foreach (var typeSymbols in ns.GetTypeMembers().Where(t => !t.Name.StartsWith("<")))
{
yield return typeSymbol;
}
foreach (var namespaceSymbol in ns.GetNamespaceMembers())
{
foreach (var typeSymbol in GetTypeSymbols(ns))
{
yield return typeSymbol;
}
}
}
The GetTypeSymbols
method walks through the namespaces and discovers all types. We then chain the result to the filter method, which ensures it implements our required interface:
private bool Filter(ITypeSymbol symbol)
{
return symbol.IsReferenceType
&& !symbol.IsAbstract
&& !symbol.IsAnonymousType
&& symbol.AllInterfaces.Any(i => i.GetFullMetadataName(i) == "Sample.IModule");
}
With GetFullMetadataName
being a utility method:
private static string GetFullMetadataName(INamespaceOrTypeSymbol symbol)
{
ISymbol s = symbol;
var builder = new StringBuilder(s.MetadataName);
var last = s;
while (!!IsRootNamespace(s))
{
builder.Insert(0, '.');
builder.Insert(0, s.MetadataName);
s = s.ContainingSymbol;
}
return builder.ToString();
}
private static bool IsRootNamespace(ISymbol symbol)
{
return symbol is INamespaceSymbol && ((INamespaceSymbol)symbol).IsGlobalNamespace;
}
Next up, module declarations in the current compilation:
private IEnumerable<string> GetModuleClassDeclarations(Compilation compilation)
{
var trees = compilation.SyntaxTrees.ToArray();
var models = trees.Select(compilation.GetSemanticModel(t)).ToArray();
for (var i = 0; i < trees.Length; i++)
{
var tree = trees[i];
var model = models[i];
var types = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().ToList();
foreach (var type in types)
{
var symbol = model.GetDeclaredSymbol(type) as ITypeSymbol;
if (symbol != null && Filter(symbol))
{
yield return GetFullMetadataName(symbol);
}
}
}
}
And that's really it! So, now at compile time, my ICompileModule
will:
- Discover all available modules
- Implement an override of my
ModuleProvider.Setup
method with all known referenced modules.
This means I can add my startup:
public class Startup
{
public ModuleProvider ModuleProvider = new ModuleProvider();
public void ConfigureServices(IServiceCollection services)
{
var descriptors = ModuleProvider.GetModules() // Ordered
.SelectMany(m => m.GetServices());
// Apply descriptors to services.
}
public void Configure(IApplicationBuilder app)
{
var modules = ModuleProvider.GetModules(); // Ordered.
// Startup code.
}
}
Massively over-engineered, quite complex, but kinda awesome I think!