Reputation:
I'm writing a C# code analyzer and running into a problem. The following source code file:
using System;
namespace J4JSoftware.Logging
{
// added to test SharpDoc
[AttributeUsage( validOn: AttributeTargets.Class | AttributeTargets.Interface, Inherited = false, AllowMultiple = true )]
public class DummyAttribute : Attribute
{
#pragma warning disable 67
// ReSharper disable once EventNeverSubscribedTo.Global
public event EventHandler<int> Ralph;
#pragma warning restore 67
#pragma warning disable 8618
public DummyAttribute( string arg1, Type arg2 )
#pragma warning restore 8618
{
}
public int TestField;
}
public interface IDummyInterface1
{
int Number { get; set; }
}
public interface IDummyInterface2 : IDummyInterface1
{
string Text { get; set; }
}
public interface IDummyInterface3<in T>
where T : DummyAttribute
{
string GetValue( T item );
bool TestGenericMethod<TMethod>()
where TMethod : class, IDummyInterface1;
}
}
compiles without any errors or warnings inside Visual Studio 2019. But it generates a CS0116 error --
A namespace cannot directly contain members such as fields or methods.
when I compile it using Roslyn. The referenced line number causing the error is line zero, "using System;".
What's causing this behavior and how do I fix it?
Upvotes: 0
Views: 97
Reputation:
Sigh. Idiot at the keyboard error. Which I'll describe here in case anyone else makes the same mistake.
To compile a project you have to first parse the source code files (and do some other things, too). I called the parser like this:
var tree = CSharpSyntaxTree.ParseText( srcFile );
but I should've called it like this:
var tree = CSharpSyntaxTree.ParseText( File.ReadAllText(srcFile) );
The parser expects source code, not a file path.
Upvotes: 3