private Object[] ReadArguments(TProtocol inProtocol) { try { int numArgs = _method.GetParameters().Length; Object[] args = new Object[numArgs]; TProtocolReader reader = new TProtocolReader(inProtocol); // Map incoming arguments from the ID passed in on the wire to the position in the // java argument list we expect to see a parameter with that ID. reader.ReadStructBegin(); while (reader.NextField()) { short fieldId = reader.GetFieldId(); IThriftCodec codec = null; if (!_parameterCodecs.TryGetValue(fieldId, out codec)) { // unknown field reader.SkipFieldData(); } else { // Map the incoming arguments to an array of arguments ordered as the java // code for the handler method expects to see them args[_thriftParameterIdToCSharpArgument[fieldId]] = reader.ReadField(codec); } } reader.ReadStructEnd(); // Walk through our list of expected parameters and if no incoming parameters were // mapped to a particular expected parameter, fill the expected parameter slow with // the default for the parameter type. int argumentPosition = 0; foreach (ThriftFieldMetadata argument in _parameters) { if (args[argumentPosition] == null) { Type argumentType = argument.ThriftType.CSharpType; if (!argumentType.Equals(typeof(void))) { args[argumentPosition] = ThriftyUtilities.GetDefaultValue(argumentType); } } argumentPosition++; } return(args); } catch (TProtocolException e) { // TProtocolException is the only recoverable exception // Other exceptions may have left the input stream in corrupted state so we must // tear down the socket. throw new TApplicationException(TApplicationException.ExceptionType.ProtocolError, e.Message); } }
private T ConstructStruct(IDictionary <short, Object> data) { // construct instance Object instance; { ThriftConstructorInjection constructor; if (!_metadata.TryGetThriftConstructorInjection(out constructor)) { throw new ThriftyException($"ReflectionThriftStructCodec need a constructor injection, for type {_metadata.StructType.FullName}"); } Object[] parametersValues = new Object[constructor.Parameters.Count()]; foreach (ThriftParameterInjection parameter in constructor.Parameters) { Object value = null; if (data.TryGetValue(parameter.Id, out value)) { parametersValues[parameter.ParameterIndex] = value; } else { parametersValues[parameter.ParameterIndex] = ThriftyUtilities.GetDefaultValue(parameter.CSharpType); } } try { instance = constructor.Constructor.Invoke(parametersValues); } catch (Exception) { throw; } } // inject fields foreach (ThriftFieldMetadata fieldMetadata in _metadata.GetFields(FieldKind.ThriftField)) { foreach (var fieldInjection in fieldMetadata.Injections.OfType <ThriftFieldInjection>()) { Object value; if (data.TryGetValue(fieldInjection.Id, out value)) { fieldInjection.Field.SetValue(instance, value); } } } // inject methods foreach (ThriftMethodInjection methodInjection in _metadata.MethodInjections) { bool shouldInvoke = false; Object[] parametersValues = new Object[methodInjection.Parameters.Count()]; foreach (ThriftParameterInjection parameter in methodInjection.Parameters) { Object value; if (data.TryGetValue(parameter.Id, out value)) { parametersValues[parameter.ParameterIndex] = value; shouldInvoke = true; } } if (shouldInvoke) { methodInjection.Method.Invoke(instance, parametersValues); } } ThriftMethodInjection builderMethod; // builder method if (_metadata.TryGetBuilderMethod(out builderMethod)) { Object[] parametersValues = new Object[builderMethod.Parameters.Count()]; foreach (ThriftParameterInjection parameter in builderMethod.Parameters) { Object value = data[parameter.Id]; parametersValues[parameter.ParameterIndex] = value; } instance = builderMethod.Method.Invoke(instance, parametersValues); if (instance == null) { throw new ThriftyException("Builder method returned a null instance"); } if (!_metadata.StructType.GetTypeInfo().IsInstanceOfType(instance)) { throw new ThriftyException( $"Builder method returned instance of type {instance.GetType().FullName}, but an instance of {_metadata.StructType.FullName} is required."); } } return((T)instance); }