Esempio n. 1
0
        /// <summary>
        /// Creates an expression reading the specified struct type.
        /// </summary>
        private static LambdaExpression CreateReaderForStruct(ThriftStruct thriftStruct)
        {
            var protocolParam = Expression.Parameter(typeof(IThriftProtocol));

            var structType = thriftStruct.TypeInfo.AsType();
            var structVar  = Expression.Variable(structType);

            var wireFields = thriftStruct.Fields.Select(f => ThriftWireField.Field(f, structVar)).ToList();

            return(Expression.Lambda(
                       Expression.Block(
                           structType,
                           new[] { structVar },

                           Expression.Assign(
                               structVar,
                               Expression.New(structType)
                               ),

                           CreateReaderForFields(protocolParam, wireFields),

                           // return value
                           structVar
                           ),
                       protocolParam
                       ));
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an expression writing the specified struct type.
        /// </summary>
        private static LambdaExpression CreateWriterForStruct(ThriftStruct thriftStruct)
        {
            var valueParam    = Expression.Parameter(thriftStruct.TypeInfo.AsType());
            var protocolParam = Expression.Parameter(typeof(IThriftProtocol));

            var methodContents = new List <Expression>
            {
                Expression.Call(
                    protocolParam,
                    Methods.IThriftProtocol_WriteStructHeader,
                    Expression.New(
                        Constructors.ThriftStructHeader,
                        Expression.Constant(thriftStruct.Header.Name)
                        )
                    )
            };

            foreach (var field in thriftStruct.Fields)
            {
                methodContents.Add(CreateWriterForField(protocolParam, ThriftWireField.Field(field, valueParam)));
            }

            methodContents.Add(Expression.Call(protocolParam, Methods.IThriftProtocol_WriteFieldStop));
            methodContents.Add(Expression.Call(protocolParam, Methods.IThriftProtocol_WriteStructEnd));

            return(Expression.Lambda(
                       Expression.Block(methodContents),
                       valueParam, protocolParam
                       ));
        }