コード例 #1
0
        public void HandleWrite(TypeHandlingContext context)
        {
            IType valueType = context.Type.GetNullableUnderlyingType();

            if (valueType == null)
            {
                throw new NotSupportedException();
            }

            var valueHandlingContext = new TypeHandlingContext(context)
            {
                Type          = valueType,
                TypeName      = valueType.GetPresentableName(context.PresentationLanguage),
                TypeOwnerName = String.Format("{0}.Value", context.TypeOwnerName)
            };

            ITypeHandler valueHandler = TypeHandlers.All.FirstOrDefault(h => h.CanHandle(valueHandlingContext));

            if (valueHandler == null)
            {
                throw new NotSupportedException();
            }

            context.Builder.AppendFormat("tmp = BitConverter.GetBytes({0}.HasValue);", context.TypeOwnerName);
            context.Builder.Append("Array.Copy(tmp, 0, bytes, index, tmp.Length);");
            context.Builder.Append("index += tmp.Length;");
            context.Builder.AppendFormat("if({0}.HasValue)", context.TypeOwnerName).Append("{");
            valueHandler.HandleWrite(valueHandlingContext);
            context.Builder.Append("}");
        }
コード例 #2
0
        public void HandleWrite(TypeHandlingContext context)
        {
            IDeclaredType valueType = context.Type.GetScalarType();

            if (valueType == null)
            {
                throw new NotSupportedException();
            }

            ITypeHandler valueHandler = TypeHandlers.All.SingleOrDefault(h => h.CanHandle(context.WithType(valueType)));

            if (valueHandler == null)
            {
                throw new NotSupportedException();
            }

            String indexName = context.Variables.Declare(Index);

            var exists = context.Variables.Declare(VariableKeys.NotNull);

            context.Builder.AppendFormat("var {0} = {1} != null;", exists, context.TypeOwnerName);
            TypeHandlers.Boolean.HandleWrite(new TypeHandlingContext(context)
            {
                TypeOwnerName = exists
            });
            context.Builder.AppendFormat("if({0})", exists);
            context.Builder.Append("{");
            TypeHandlers.Int32.HandleWrite(new TypeHandlingContext(context)
            {
                TypeName      = TypeHandlers.Int32.TypeName,
                TypeOwnerName = String.Format("{0}.Length", context.TypeOwnerName)
            });
            context.Builder.AppendFormat("if({0}.Length > 0)", context.TypeOwnerName);
            context.Builder.Append("{");
            context.Builder.AppendFormat("for(Int32 {0} = 0; {0} < {1}.Length; {0}++)", indexName, context.TypeOwnerName);
            context.Builder.Append("{");
            valueHandler.HandleWrite(new TypeHandlingContext(context)
            {
                TypeName      = valueType.GetPresentableName(context.PresentationLanguage),
                TypeOwnerName = String.Format("{0}[{1}]", context.TypeOwnerName, indexName)
            });
            context.Builder.Append("}}}");
            context.Variables.Dispose(Index);
        }
コード例 #3
0
        protected override void Generate(CSharpGeneratorContext context,
                                         IMethodDeclaration declaration,
                                         IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                                         CSharpElementFactory factory)
        {
            var owner = (IParametersOwner)declaration.DeclaredElement;

            if (owner == null)
            {
                return;
            }

            var ctx = new TypeHandlingContext(context);

            ctx.Builder.Append("{");
            ctx.Builder.AppendFormat("var index = startIndex;");
            ctx.Builder.AppendFormat("Byte[] tmp;");
            if (elements.Count > 0)
            {
                foreach (var element in elements)
                {
                    ctx.Resolve(element.DeclaredElement);

                    ITypeHandler handler = TypeHandlers.All.FirstOrDefault(h => h.CanHandle(ctx));
                    if (handler != null)
                    {
                        handler.HandleWrite(ctx);
                    }
                }
            }
            ctx.Builder.Append("return index;").Append("}");

            IBlock body = factory.CreateBlock(ctx.Builder.ToString(), ctx.Args.ToArray());

            declaration.SetBody(body);
        }