Beispiel #1
0
        //internal PropertyInfo[] TabProperties
        //{
        //    get
        //    {
        //        List<PropertyInfo> props = new List<PropertyInfo>();
        //        foreach (var fieldInfo in typeof(T).GetProperties())
        //        {
        //            if (fieldInfo.GetCustomAttributes(typeof(TabColumnAttribute), true).Length > 0)
        //            {
        //                props.Add(fieldInfo);
        //            }
        //        }
        //        return props.ToArray();
        //    }
        //}

        /// <summary>
        /// Auto parser with class's definition fields (poor performance warning)
        /// </summary>
        /// <param name="tableRow"></param>
        /// <param name="cellStrs"></param>
        protected void AutoParse(TableFileRow tableRow, string[] cellStrs)
        {
            var type     = tableRow.GetType();
            var okFields = new List <FieldInfo>();

            //解析field
            foreach (FieldInfo field in AutoTabFields)
            {
                if (!HasColumn(field.Name))
                {
                    OnException(TableFileExceptionType.NotFoundHeader, type.Name, field.Name);
                    continue;
                }
                okFields.Add(field);
            }

            foreach (var field in okFields)
            {
                var fieldName = field.Name;
                var fieldType = field.FieldType;

                //通过TableRowFieldParser自动解析string to fieldType
                var methodName = string.Format("Get_{0}", fieldType.Name);
                var method     = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
                if (method != null)
                {
                    // 找寻FieldName所在索引
                    int index = Headers[fieldName].ColumnIndex;
                    // default value
                    //string szType = "string";
                    string defaultValue = "";
                    var    headerDef    = Headers[fieldName].HeaderMeta;
                    if (!string.IsNullOrEmpty(headerDef))
                    {
                        var defs = headerDef.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        //if (defs.Length >= 1) szType = defs[0];
                        if (defs.Length >= 2)
                        {
                            defaultValue = defs[1];
                        }
                    }

                    field.SetValue(tableRow, method.Invoke(tableRow, new object[]
                    {
                        cellStrs[index], defaultValue
                    }));
                }
                else
                {
                    OnException(TableFileExceptionType.NotFoundGetMethod, methodName);
                }
            }
        }
Beispiel #2
0
        //internal PropertyInfo[] TabProperties
        //{
        //    get
        //    {
        //        List<PropertyInfo> props = new List<PropertyInfo>();
        //        foreach (var fieldInfo in typeof(T).GetProperties())
        //        {
        //            if (fieldInfo.GetCustomAttributes(typeof(TabColumnAttribute), true).Length > 0)
        //            {
        //                props.Add(fieldInfo);
        //            }
        //        }
        //        return props.ToArray();
        //    }
        //}

        //解析TableFileRow类的所有成员。将cellStrs数据反射保存到TableFileRow
        protected void AutoParse(TableFileRow tableRow, string[] cellStrs)
        {
            var type     = tableRow.GetType();
            var okFields = new List <FieldInfo>();

            //遍历TableFileRow的所有成员,容错
            foreach (FieldInfo field in AutoTabFields)
            {
                if (!HasColumn(field.Name))
                {
                    //类中没有这个成员名称,报错
                    OnException(TableFileExceptionType.NotFoundHeader, type.Name, field.Name);
                    continue;
                }
                okFields.Add(field);
            }

            //遍历所有成员
            foreach (var field in okFields)
            {
                //成员名称
                var fieldName = field.Name;
                var fieldType = field.FieldType;

                //判断该成员是否有Get方法
                var methodName = string.Format("Get_{0}", fieldType.Name);
                var method     = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
                if (method != null)
                {
                    //有Get方法

                    // FieldName所在索引
                    int index = Headers[fieldName].ColumnIndex;

                    //该成员默认值。如果第二行的声明,根据,分割后有两项,则第一项是默认值
                    string defaultValue = "";
                    var    headerDef    = Headers[fieldName].HeaderMeta;
                    if (!string.IsNullOrEmpty(headerDef))
                    {
                        var defs = headerDef.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        //if (defs.Length >= 1) szType = defs[0];
                        if (defs.Length >= 2)
                        {
                            defaultValue = defs[1];
                        }
                    }

                    //调用TableFileRow的Get方法,数据保存
                    field.SetValue(tableRow, method.Invoke(tableRow, new object[]
                    {
                        //数据
                        cellStrs[index], defaultValue
                    }));
                }
                else
                {
                    //没有Get方法,报错
                    OnException(TableFileExceptionType.NotFoundGetMethod, methodName);
                }
            }
        }