-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SemanticModel.IsAccessible returning true for inaccessible protected member #43696
Comments
|
@Joe4evr Then how do you check whether or not replacing This isn't reflection and it doesn't even involve IL at this level, only C# syntax rules. I'm not sure the .NET design meeting was about this. |
Repro code btw: Click to expand<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.5.0" />
</ItemGroup>
</Project> using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.IO;
using System.Linq;
public static class Program
{
public static void Main()
{
var syntaxTree = CSharpSyntaxTree.ParseText(@"
class Base
{
protected void M1() { }
}
class C : Base
{
class Nested
{
void M2()
{
typeof(Base).GetMethod(""M1"");
}
}
}");
var compilation = CSharpCompilation.Create(
"Repro",
new[] { syntaxTree },
new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) },
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var emitResult = compilation.Emit(Stream.Null);
if (!emitResult.Success) throw new NotImplementedException("Repro must not have compilation errors.");
var stringLiteralSyntax = syntaxTree.GetRoot().DescendantNodes().OfType<LiteralExpressionSyntax>().Single();
var model = compilation.GetSemanticModel(syntaxTree, ignoreAccessibility: false);
var baseM1Symbol = compilation.Assembly.GlobalNamespace.GetTypeMembers("Base").Single().GetMembers("M1").Single();
// Should print False but prints True
Console.WriteLine(model.IsAccessible(stringLiteralSyntax.SpanStart, baseM1Symbol));
}
} |
Ah, I see what's going on now. OP probably should've been a bit more clear about the intention, rather than posting only a snippet of code with no elaboration. |
[jcouv update:] Thanks @jnm2 for clarifying the repro (below)
The text was updated successfully, but these errors were encountered: