using EdjCase.JsonRpc.Router.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace EdjCase.JsonRpc.Router.MethodProviders
{
///
/// Method provider finding all public methods through reflection
///
public class ControllerPublicMethodProvider : IRpcMethodProvider
{
///
/// List of types to match against
///
public IReadOnlyList Types { get; }
private List methodCache { get; set; }
/// List of types to match against
public ControllerPublicMethodProvider(List types)
{
if (types == null || !types.Any())
{
throw new ArgumentException("At least one type must be specified.", nameof(types));
}
this.Types = types;
}
/// Type to match against
public ControllerPublicMethodProvider(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
this.Types = new List { type };
}
public List GetRouteMethods()
{
if (this.methodCache == null)
{
var methods = new List();
foreach (Type type in this.Types)
{
List publicMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
//Ignore ToString, GetHashCode and Equals
.Where(m => m.DeclaringType != typeof(object))
.ToList();
methods.AddRange(publicMethods);
}
this.methodCache = methods;
}
return this.methodCache;
}
}
}