I´m creating a roslyn analyzer to check the usage of an attribute from my framework code.
Example:
Framework.csproj
public class ModuleAttribute : Attribute { }
Framework.Analyzer.csproj
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class IsModuleAPublicClassAnalyzer : DiagnosticAnalyzer
{ ... }
Framework.Analyzer.Test.csproj
[Fact]
public async Task MyTestMethod()
{
string test = @"
using Framework;
namespace MyNamespace;
[Module]
public class MyConcreteModule
{
}
";
DiagnosticResult expected = VerifyCs
.Diagnostic(AsyncPropertySetterAnalyzer.DiagnosticId)
.WithLocation(line: 6, column: 0);
await new CSharpAnalyzerTest<IsModuleAPublicClassAnalyzer, XUnitVerifier>
{
TestState =
{
Sources = { test },
ExpectedDiagnostics = { expected }
}
}
.RunAsync();
}
How can I add a reference to Framework.dll
in the test codesnippet?
All projects are in the same solution.
Update 1
I noticed that it is possible to add additional MetadataReference
s like this:
Framework.Analyzer.Test.csproj
[Fact]
public async Task MyTestMethod()
{
string test = @"
using Framework;
namespace MyNamespace;
[Module]
public class MyConcreteModule
{
}
";
DiagnosticResult expected = VerifyCs
.Diagnostic(AsyncPropertySetterAnalyzer.DiagnosticId)
.WithLocation(line: 6, column: 0);
await new CSharpAnalyzerTest<IsModuleAPublicClassAnalyzer, XUnitVerifier>
{
TestState =
{
Sources = { test },
ExpectedDiagnostics = { expected },
AdditionalReferences =
{
MetadataReference.CreateFromFile(typeof(ModuleAttribute).Assembly.Location)
}
}
}
.RunAsync();
}
Now I get that error:
error CS1705: Assembly 'Framework' with identity 'Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=29fe1ef4929b04aa' uses 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Runtime' with identity 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Framework.csproj
and Framework.Analyzer.Test.cspoj
have
target framework net7.0
Framework.Analyzer.csproj
is netstandard2.0