Using the C# fluent API

How it works

All configuration of the server process is done using a builder object. The process is as follows

  1. Create the builder object
  2. Configure which methods should be exposed to Excel
  3. Create a server object
  4. Start the server

The methods on the builder object returns the object so that all configuration commands can be chained.

Create the builder and add some methods

The AddFunctions property of the BridgeServiceBuilder returns a BridgeFunctionConfiguration object which can be used to add methods to the server.

By adding a C# method to the server, you expose it as a function (formula) in Excel

Let’s say that you have a class that contains some public and static methods. You can add them to the server by the following code.

var builder = new BridgeServiceBuilder()
  .AddFunctions.FromType<CustomFunctions>();

If CustomFunctions is a static class, you need to use the alternative call

  builder.AddFunctions(typeof(CustomFunctions))

There are a number of different methods available for adding methods.

Add methods from types

The following methods add all public and static methods from respective types.

Add methods from objects

The following methods add all public instance methods invoked on the target object.

The first method adds all methods from the type target.GetType(), the following

Add methods explicitly

Methods can be added from an enumeration of MethodInfo objects for a more granular setup

Chaining commands

All of the FromXxx methods above return the BridgeServiceBuilder object, and more functions can by using AddFunction again, e.g.

  var builder = new BridgeServiceBuilder()
      .AddFunctions.FromType<CustomFunctions>()
      .AddFunctions.FromType(typeof(System.Math));

Configuring how methods are added

Specify which functions to include

By default all (public) static methods from the type are added. This can be controlled by the BridgeMemberFlags option.

    [Flags]
    public enum BridgeMemberFlags
    {
        None = 0,
        Instance = 1,
        Static = 2,
        Methods = 4,
        Properties = 8
    }

Configuring a prefix for functions

A common use case is adding a prefix in Excel to a set of methods

E.g. add a prefix MYFUNCS. to all methods in CustomFunction with

    var builder = new BridgeServiceBuilder()
        .AddFunctions
            .WithPrefix("MYFUNCS.")
            .FromType<CustomFunctions>()

Configuring the naming of functions

There are three methods available to control the way methods are added to the server. These are called before calling the FromXxx methods

The namingOptionsBuilder defines the naming of a function in Excel. This function will be called for each added method.

    public class NamingOptions
    {
        public string Name = null;
        public string Prefix = null;
        public string Category = null;
        public string Description = null;
    }

NamingOptions contains four fields, where a non-null value for any of them will override the default naming.

To add methods to a specific category in Excel, you could use e.g.

var builder = new BridgeServiceBuilder()
  .AddFunctions
    .WithNaming(memberInfo => new NamingOptions { Category = "My functions" })
    .FromType<CustomFunctions>()

Read more