Search code examples
c#intellisensemicrosoft.codeanalysisroslynpad

Is there a way to "cap" RoslynPad's Roslyn's IntelliSense?


I'm actually integrating the amazing RoslynPad into a WinForms application and working damn well.

The point of the integration is allowing the user to type in some C# code so it can be used in a future.

Thing is I'm interested on "capping" the user so he could just use some System or even LinQ functions. I don't want to allow the user to think he is allowed to use System.IO and others. Of course I can't prevent him/her typing System.IO.File.Delete, but will surely help if the System.IO's Assembly is not loaded into the RoslynPad's IntelliSense.

The source code typed by the user is going to be compiled locally before being saved into the DB. I'm adding just a few and necessary Assemblies for the compilation, so if System.IO it won't compile, of course.

As I explained, I just want to cap the Intellisense, so they don't think they have access to almost the whole .NET Framework.

EDIT: Added the actual implementation actually done. I'm loading "RoslynPad.Roslyn.Windows" and "RoslynPad.Editor.Windows" assemblies to the editor.

private RoslynCodeEditor _editor;
private void InitializeEditor(string sourceCode)
{
    if (string.IsNullOrWhiteSpace(sourceCode))
        sourceCode = string.Empty;
    _editor = new RoslynCodeEditor();
    var workingDirectory = Directory.GetCurrentDirectory();
    var roslynHost = new RoslynHost(additionalAssemblies: new[]
    {
        Assembly.Load("RoslynPad.Roslyn.Windows"),
        Assembly.Load("RoslynPad.Editor.Windows")
    });

    _editor.Initialize(roslynHost, new ClassificationHighlightColors(), workingDirectory, sourceCode);
    _editor.FontFamily = new System.Windows.Media.FontFamily("Consolas");
    _editor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#");
    _editor.FontSize = 12.75f;
    elementHost1.Child = _editor;
    this.Controls.Add(elementHost1);
}

Solution

  • You can use pass a RoslynHostReferences instance to the RoslynHost constructor, and decide which assemblies and namespaces are imported by default.

    You could use the same logic as Default, just remove System.IO.Path from the type list.

    Note that System.IO is not an assembly, but rather a namespace, which is in the core library, so there's no simple way to completely remove it.