Interface-Method: IsSealed does not work as expected
Version Used:
Steps to Reproduce:
Execute:
using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
var tree = ParseSyntaxTree("""
using System;
interface IFoo
{
sealed void SealedMethod()
{
Console.WriteLine("IFoo.SealedMethod");
}
}
""");
var refApis = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => !a.IsDynamic)
.Select(a => MetadataReference.CreateFromFile(a.Location));
var compilation = CSharpCompilation.Create("something", new[] { tree }, refApis);
compilation = compilation.WithOptions(compilation.Options.WithOutputKind(OutputKind.DynamicallyLinkedLibrary));
var method = compilation.SyntaxTrees.First().GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().Single();
var symbol = compilation.GetSemanticModel(method.SyntaxTree).GetDeclaredSymbol(method);
Console.WriteLine(symbol.IsSealed); //<-- returns false
Expected Behavior: Expected output: True
Actual Behavior: Prints: False
Is there any API to get the "correct" IsSealed value?
The "Sealed" value seems to be encoded with the newslot flag - but that detail should not be part of the public API, or?
Interesting. This might be a bug. Compiler team would have to check. In the meantime, you can check for this syntactically on the method decl node you searched for.
@333fred can you confirm whether or not this behavior is intended?
@AlekseyTs would be better to comment here.
However, I'm guessing this is intentional, as we don't emit final into the IL here.
This is by design. For interface members, sealed modifier means not-virtual.