filipw / Strathweb.Samples.CSharp.NoVisibilityChecks

Example of compiling and running C# code without visibility checks.
MIT License
46 stars 5 forks source link

Does not work "on my computer" :-) #1

Open sturlath opened 5 years ago

sturlath commented 5 years ago

Hi I just downloaded the example after I read your blog post, after witch I tried to use the [IgnoresAccessChecksTo] attribute without luck.

And after downloading your example and trying to build it I get the same access protection level errors.

Am I missing something here? Should't it just build or is there some part missing? Can't figure it out.

This is my VS info if that helps Microsoft Visual Studio Professional 2017 Version 15.8.2 VisualStudio.15.Release/15.8.2+28010.2016 Microsoft .NET Framework Version 4.7.03056

sturlath commented 5 years ago

Ok...ok... I just run the Compiler project and that in affect runs the code in the CrazyProgram project even though I get the protection level access errors there.

So I basically need the code in the Compiler Main to run the code and not just adding [assembly: IgnoresAccessChecksTo("Calculator")]? I will have to see how I can use this code with my tests in a generic manner.

I was hoping to be able to add this attribute to my unit test namespace and have access to all the private methods.

But anyway I need be able to suppress these errors if the solution is to build. How do I do that?

Again... am I missing something? :-)

image

sturlath commented 5 years ago

Another update. I managed to use this nuget package to get rid of all the errors and access the internal classes but not the one on the private method. Thought you might find that package interesting

image

sturlath commented 5 years ago

Are we also just talking about giving access to internal methods and not private? Just like the [InternalsVisibleToAttribute]

The IgnoresAccessChecksToAttribute nuget only works with internal.

filipw commented 5 years ago

Are we also just talking about giving access to internal methods and not private?

As mentioned in the post, the compiler also need BinderFlags.IgnoreAccessibility to be set to be able to work with privates.

sturlath commented 5 years ago

Yes I’m seeing it now thanks. But how about the build/parsing errors? Any change I can get rid of them?

filipw commented 5 years ago

Yes I’m seeing it now thanks. But how about the build/parsing errors? Any change I can get rid of them?

you can get rid of them only if you modify the compiler that is powering the language service. This is really not possible (AFAIK) in Visual Studio because you can't load custom Roslyn build in there.

you can do that in OmniSharp/VS Code though, by making the same change as here https://github.com/filipw/Strathweb.Samples.CSharp.NoVisibilityChecks/blob/master/Compiler/Program.cs#L24-L28 over here https://github.com/OmniSharp/omnisharp-roslyn/blob/ba7909c19ccd6513cbbcbf60f39339c790fe75b6/src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfoExtensions.cs#L15-L50

Denis535 commented 4 years ago

Cometary customizes Roslyn via DiagnosticAnalyzer and Ryder. May be it can be possible to change CSharpCompilationOptions with the same way?

But it would be perfect to inject via cecil into Csc constructor the next code:

Arguments.CompilationOptions = Arguments.CompilationOptions.WithMetadataImportOptions(MetadataImportOptions.All).WithTopLevelBinderFlags(BinderFlags.IgnoreAccessibility);