public void PrepareMethod <T>(T instance, CommandMessage command, out MethodInfo methodInfo, out object[] callArguments) where T : RtmpController { if (!Controllers.TryGetValue(instance.GetType(), out var methods)) { throw new EntryPointNotFoundException(); } foreach (var method in methods) { if (method.MethodName != command.ProcedureName) { continue; } var arguments = new object[method.Parameters.Count]; var i = 0; foreach (var para in method.Parameters) { if (para.IsCommandObject) { arguments[i] = command.CommandObject; i++; } else if (para.IsFromCommandObject) { var commandObj = command.CommandObject; object val = null; if (!commandObj.Fields.TryGetValue(para.CommandObjectKey, out val) && !commandObj.DynamicFields.TryGetValue(para.CommandObjectKey, out val)) { if (para.IsOptional) { arguments[i] = Type.Missing; i++; } else { break; } } if (para.ParameterType.IsAssignableFrom(val.GetType())) { arguments[i] = val; i++; } else { if (para.IsOptional) { arguments[i] = Type.Missing; i++; } else { break; } } } else if (para.IsFromOptionalArgument) { var optionArguments = command.GetType().GetProperties().Where(p => p.GetCustomAttribute <OptionalArgumentAttribute>() != null).ToList(); if (para.OptionalArgumentIndex >= optionArguments.Count) { if (para.IsOptional) { arguments[i] = Type.Missing; i++; } else { break; } } else { arguments[i] = optionArguments[i].GetValue(command); i++; } } } if (i == arguments.Length) { methodInfo = method.Method; callArguments = arguments; return; } } throw new EntryPointNotFoundException(); }
public void Notify(CommandMessage message) { _log.Info($"{message.GetType().Name} for action {message.ActionId} received."); }