public override void Populate(CSharpGeneratorContext context)
        {
            // use context.ProvidedElements.AddRange to add new
            // generator elements (e.g., GeneratorDeclaredElement<T>)
            IClassLikeDeclaration typeDeclaration = context.ClassDeclaration;
            ITypeElement          typeElement     = typeDeclaration.DeclaredElement;

            ListHandler.Initialize(context);
            MethodGeneratorBase.Initialize(context);
            GetBytesMethodGenerator.Initialize(context);
            SetBytesMethodGenerator.Initialize(context);

            if (typeElement is IClass || typeElement is IStruct)
            {
                var ctx = new TypeHandlingContext(context);

                foreach (ITypeMember member in typeElement.GetMembers())
                {
                    ITypeOwner owner = null;

                    var field = member as IField;
                    if (field != null)
                    {
                        if (field.GetAccessRights() != AccessRights.PRIVATE &&
                            !field.IsConstant &&
                            !field.IsReadonly &&
                            !field.IsStatic)
                        {
                            owner = field;
                        }
                    }

                    var property = member as IProperty;
                    if (property != null)
                    {
                        if (property.IsReadable &&
                            property.IsWritable &&
                            !property.IsStatic)
                        {
                            owner = property;
                        }
                    }

                    if (owner != null)
                    {
                        ctx.Resolve(owner);

                        if (TypeHandlers.All.Any(h => h.CanHandle(ctx)))
                        {
                            context.ProvidedElements.Add(new GeneratorDeclaredElement <ITypeOwner>(owner));
                        }
                    }
                }
            }
        }
        private static 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);

            var size = ctx.GetSizeVariableName();

            ctx.Builder.Append("{");
            ctx.Builder.AppendFormat("var {0} = 0;", size);
            if (elements.Count > 0)
            {
                foreach (var element in elements)
                {
                    ctx.Resolve(element.DeclaredElement);

                    ITypeHandler handler = TypeHandlers.All.SingleOrDefault(h => h.CanHandle(ctx));
                    if (handler != null)
                    {
                        handler.HandleGetSize(ctx);
                    }
                }
            }
            ctx.Builder.AppendFormat("return {0};", size);
            ctx.Builder.Append("}");

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

            declaration.SetBody(body);
        }
        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.Append("var index = startIndex;");
            ctx.Builder.Append("Byte[] tmp;");
            if (elements.Count > 0)
            {
                foreach (var element in elements)
                {
                    ctx.Resolve(element.DeclaredElement);

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

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

            declaration.SetBody(body);
        }