Skip to content

Roslyn Plugin Catalog

Mikael Koskinen edited this page Aug 10, 2020 · 7 revisions

Roslyn Plugin Catalog is available as a separate Nuget-package:

NuGet Version

Roslyn Plugin Catalog allows you to use either C#'s scripting version or full-blown C#-code as a plugin. Here's a simple example of using the script version:

var script = "var x = \"Hello from Roslyn Plugin\"; return x;";
var roslynScriptCatalog = new RoslynPluginCatalog(script);

Here's an example where C# is used instead of its scripting version:

var code = @"public class MyClass
       {
           public void RunThings()
           {
               var y = 0;
               var a = 1;

               a = y + 10;
           
               Debug.WriteLine(y + a);
           }
       }";

var roslynCatalog = new RoslynPluginCatalog(script);

Options

Roslyn Catalog supports multiple options. These help to finetune the desired outcome. Options can be passed to the catalog as a constructor parameter:

var options = new RoslynPluginCatalogOptions() { PluginName = "MyPlugin", };
var catalog = new RoslynPluginCatalog(code, options);

The following options can be configured when using Roslyn Plugin Catalog:

PluginName

Name of the plugin. Defaults to RoslynCode.

PluginVersion

Version of the plugin. Defaults to 1.0.0.

TypeName

When using scripting, the script is wrapped inside a type. Defaults to GeneratedType.

NamespaceName

When using scripting, the script is wrapped inside a type. This controls the namespace of the type. Defaults to GeneratedNamespace.

MethodName

When using scripting, the script is wrapped inside a method. Defaults to Run.

The default TypeName, NamespaceName and MethodName mean that when a plugin is from a C# script, it can be executed through GeneratedNamespace.GeneratedType.Run.

ReturnsTask

By default the scripts return a task. In practice this means that the following code returns a Task

var code = "var x = \"hello\"; return x;";

AdditionalReferences

Roslyn catalog generates types from a code. By default the compiler in this process references only the System.Private.CoreLib. AdditionalReferences can be used to provide more references.

AdditionalNamespaces

Roslyn catalog generates types from a code. By default the code uses the following namespaces:

  • System
  • System.Diagnostics
  • System.Threading.Tasks
  • System.Text
  • System.Collections
  • System.Collections.Generic

With AdditionalNamespaces -property more namespaces can be provided. compiler in this process references only the System.Private.CoreLib. AdditionalReferences can be used to provide more references.

Here's an example which shows how additional references and namespaces can be used:

var code = @"public class MyClass
       {
           private ExternalService _service;
           public MyClass(ExternalService service)
           {
                _service = service;
           } 

           public string RunThings()
           {
                var result = JsonConvert.SerializeObject(15);
                result += _service.DoWork();
                return result; 
           }
       }";

var options = new RoslynPluginCatalogOptions()
{
    PluginName = "MyPlugin",
    PluginVersion = new Version("1.5.0.0"),
    AdditionalReferences = new List<Assembly>() { typeof(Newtonsoft.Json.JsonConvert).Assembly, typeof(ExternalService).Assembly },
    AdditionalNamespaces = new List<string>() { "Newtonsoft.Json", "WebAppWithRoslyn" }
};

var roslynCodeCatalog = new RoslynPluginCatalog(code, options);