ExportAttribute
[Export<IHandlerBase>] indicates that handler is to be exported.
[Export<IHandlerBase>]
public class TypeHandler : IHandler<string, Type>
{
public void Handle(IQuery<string, Type> query)
{
// Already handled
if (query.Handled()) return;
// Find type
Type? type = Type.GetType(query.Request);
// No type found
if (type == null) return;
// Assign result
query.Response.SetValue(type);
}
}
HandlerExports.Assembly and HandlerExports.AssemblyCached are providers that scan for [Export<IHandlerBase>] annotated handlers. Cached version uses weak-keyed cache that allows GC.
// Get assembly
Assembly assembly = typeof(handler_export).Assembly;
// Get [Export<IHandlerBase>] handlers
IHandlerBase[] handlers = HandlerExports.Assembly[assembly];
Each class library should have a facade class that holds its exported handlers. For Avalanche.Service the facade is ServiceHandlers.
// Handlers of Avalanche.Service
IHandlerBase[] handlers = ServiceHandlers.Instance.HandlerBases;
Third party class libraries can create a facade class from base type HandlerInfoProvider.
public class MyHandlers : HandlerInfoProvider
{
/// <summary>Singleton</summary>
static readonly MyHandlers instance = new MyHandlers().SetReadOnly();
/// <summary>Singleton</summary>
public static MyHandlers Instance => instance;
/// <summary></summary>
public MyHandlers() : base(HandlerExports.Assembly[Assembly.GetExecutingAssembly()]) { }
}
HandlerExports.Of<T> seaches for all the exported handlers in the type T's assembly. Class library can define a dummy Anchor class, that can be used for refering at an assembly. Uses weak-keyed cache that to allows GC.
// Handlers of the assembly
IHandlerBase[] handlers = HandlerExports.Of<Anchor>();
/// <summary>Dummy class that is used to get reference to the Assembly.</summary>
public class Anchor { }
Full Example
Full example
using System;
using System.Reflection;
using Avalanche.Service;
using Avalanche.Utilities;
public class handler_export
{
public static void Run()
{
{
// <01>
// Get assembly
Assembly assembly = typeof(handler_export).Assembly;
// Get [Export<IHandlerBase>] handlers
IHandlerBase[] handlers = HandlerExports.Assembly[assembly];
// </01>
}
{
// <02>
// Handlers of Avalanche.Service
IHandlerBase[] handlers = ServiceHandlers.Instance.HandlerBases;
// </02>
}
{
// <03>
// Handlers of the assembly
IHandlerBase[] handlers = HandlerExports.Of<Anchor>();
// </03>
}
}
// <04>
/// <summary>Dummy class that is used to get reference to the Assembly.</summary>
public class Anchor { }
// </04>
// <99>
[Export<IHandlerBase>]
public class TypeHandler : IHandler<string, Type>
{
public void Handle(IQuery<string, Type> query)
{
// Already handled
if (query.Handled()) return;
// Find type
Type? type = Type.GetType(query.Request);
// No type found
if (type == null) return;
// Assign result
query.Response.SetValue(type);
}
}
// </99>
// <100>
public class MyHandlers : HandlerInfoProvider
{
/// <summary>Singleton</summary>
static readonly MyHandlers instance = new MyHandlers().SetReadOnly();
/// <summary>Singleton</summary>
public static MyHandlers Instance => instance;
/// <summary></summary>
public MyHandlers() : base(HandlerExports.Assembly[Assembly.GetExecutingAssembly()]) { }
}
// </100>
}