Ejemplo n.º 1
0
        private static CsvString Wrap(CsvString writer, int count, HCsvStringContext info)
        {
            int size = info.MinSize * count + info.GetHeaderSize();

            char[] buffer = CharsBufferManager.GetBuffer(size);
            writer = new CsvString(buffer);
            writer.WriteHeader(info.Names);
            return(writer);
        }
Ejemplo n.º 2
0
        public static string ToCsv(object value)
        {
            if (value == null)
            {
                return(string.Empty);
            }
            Type              type   = value.GetType();
            CsvString         writer = null;
            HCsvStringContext info   = null;

            if (type.IsArray)
            {
                Array ar = (Array)value;
                if (ar.Length == 0)
                {
                    return(null);
                }
                Type cType = null;
                for (int i = 0; i < ar.Length; i++)
                {
                    if (ar.GetValue(i) != null)
                    {
                        cType = ar.GetValue(i).GetType();
                        info  = GetContext(cType);
                        break;
                    }
                }
                writer = Wrap(writer, ar.Length, info);
                if (type.FullName == "System.Object[]")
                {
                    for (int i = 0; i < ar.Length; i++)
                    {
                        object item = ar.GetValue(i);
                        if (item != null)
                        {
                            if (cType == item.GetType())
                            {
                                info.Serializer(writer, item);
                            }
                            else
                            {
                                writer.WriteNullLine(info.Names.Count);
                            }
                        }
                        else
                        {
                            writer.WriteNullLine(info.Names.Count);
                        }
                        writer.WriteNewLine();
                    }
                }
                else
                {
                    for (int i = 0; i < ar.Length; i++)
                    {
                        object item = ar.GetValue(i);
                        if (item != null)
                        {
                            info.Serializer(writer, item);
                        }
                        else
                        {
                            writer.WriteNullLine(info.Names.Count);
                        }
                        writer.WriteNewLine();
                    }
                }
            }
            else if (type.IsGenericType)
            {
                Type[] types = type.GenericTypeArguments;
                if (types.Length == 1)
                {
                    info = GetContext(types[0]);
                    IList list = value as IList;
                    if (list != null)
                    {
                        writer = Wrap(writer, list.Count, info);
                        for (int i = 0; i < list.Count; i++)
                        {
                            object item = list[i];
                            if (item != null)
                            {
                                info.Serializer(writer, item);
                            }
                            else
                            {
                                writer.WriteNullLine(info.Names.Count);
                            }
                            writer.WriteNewLine();
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException("not support multi generic type!");
                }
            }
            else
            {
                //直接处理枚举模式
                //bool isFirst = true;
                //foreach (object item in ar)
                //{
                //    if (isFirst)
                //    {
                //        info = GetContext(item.GetType());
                //        int size = info.MinSize * ar.Length + info.GetHeaderSize();
                //        char[] buffer = CharsBufferManager.GetBuffer(size);
                //        writer = new CsvString(buffer);
                //        writer.WriteHeader(info.Names);
                //        isFirst = false;
                //    }
                //    info.Serializer(writer, item);
                //    writer.WriteNewLine();
                //}
            }
            CharsBufferManager.SetBuffer(writer.GetBuffer());
            return(writer.ToString());
        }
Ejemplo n.º 3
0
 internal CsvString(CsvString stream)
 {
     this._buffer  = stream._buffer;
     this.position = stream.position;
     this.sets     = stream.sets;
 }