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
- Create the builder object
- Configure which methods should be exposed to Excel
- Create a server object
- 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.
-
FromType<T>()
Add all static methods from type
typeof(T)
. -
FromType(Type type)
Add all static methods from type
type
. -
FromType(IEnumerable<Type> types)
Add all static methods from the enumeration of types.
Add methods from objects
The following methods add all public instance methods invoked on the target
object.
-
FromObject(object target)
Add all methods defined in
target.GetType()
and invoke them ontarget
. -
FromObject<T>(T target)
Add all methods from type
typeof(T)
and invoke them ontarget
, wheretarget
is either of typeT
or a type derived from, or implementingT
. This is useful when only the methods on e.g. a specific interface should be added to the server. -
FromObject(Type type, object target)
Add all methods from type
type
and invoke them ontarget
.
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
FromMethods(IEnumerable<MethodInfo> methods)
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
ForMembers(BridgeMemberFlags flags)
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
WithPrefix(string prefix)
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
WithNaming(Func<MemberInfo, NamingOptions> namingOptionsBuilder)
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>()