0

I'm trying to make a diagnostic analyzer that ensures that there are no non-framework assemblies referenced within certain projects.

So far, I have this:

    public override void Initialize(AnalysisContext context)
    {
        context.EnableConcurrentExecution();
        context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics );


        context.RegisterCompilationAction(actionContext =>
        {
            // if (!actionContext.Compilation.Assembly.Name.EndsWith(".Schema"))
            // {
            //     return;
            // }
            
            var references = actionContext.Compilation.ReferencedAssemblyNames;
            
            // Check if any are non-framework assemblies
                       
            // ReportDiagnostic()
        });
    }

Which yields a list that looks like this in references:

'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed,System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

Now, a quick and dirty approach would seem to be to check for anything that doesn't start with System. and isn't mscorlib. However, this seems to me to be suboptimal.

Is there a proper way to do this? Some sort of Assembly.IsFrameworkProvided property somewhere?

This question is super-old and doesn't seem to provide any better options, I'm hoping that there's a more modern option.

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81

0 Answers0