public Handler BuildJavaScriptHandler <T>(HandlerInfo info) { var @namespace = info.Namespace ?? "module"; var entrypoint = info.Entrypoint ?? "exports"; var code = info.Code ?? $"{@namespace}.{entrypoint} = {NoJavaScriptCodeHandler}"; var md5 = MD5.Create(); var inputBytes = Encoding.UTF8.GetBytes(code); var hash = md5.ComputeHash(inputBytes); var sb = new StringBuilder(); foreach (var c in hash) { sb.Append(c.ToString("X2")); } var moduleName = $"{sb}.js"; File.WriteAllText(moduleName, code); return((t, p) => _nodeServices.InvokeAsync <T>(moduleName, p).Result); }
public Handler BuildCSharpHandler(string assemblyName, HandlerInfo info, DelegateBuildStrategy strategy = DelegateBuildStrategy.MethodInfo, params Assembly[] dependencies) { var type = BuildType(assemblyName, info, dependencies); var function = info.Function ?? "Execute"; var methodInfo = type?.GetMethod(function, BindingFlags.Public | BindingFlags.Static); if (methodInfo == null) { return(null); } switch (strategy) { case DelegateBuildStrategy.MethodInfo: { return((t, p) => methodInfo.Invoke(t, p)); } case DelegateBuildStrategy.Expression: { var parameters = methodInfo.GetParameters(); var arguments = parameters.Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToList(); var methodCall = Expression.Call(methodInfo.IsStatic ? null : Expression.Parameter(typeof(object), "instance"), methodInfo, arguments); var lambda = Expression.Lambda(Expression.Convert(methodCall, typeof(object)), arguments); var d = lambda.Compile(); return(methodInfo.IsStatic ? (Handler)((t, p) => d.DynamicInvoke(p)) : (t, p) => d.DynamicInvoke(t, p)); } case DelegateBuildStrategy.ObjectExecutor: { var executor = ObjectMethodExecutor.Create(methodInfo, type.GetTypeInfo()); return((t, p) => executor.Execute(t, p)); } default: throw new ArgumentOutOfRangeException(nameof(strategy), strategy, null); } }