示例#1
0
        public static void SetNullIdentity(object objGraph)
        {
            if (objGraph == null || _ignoreTypes.Contains(objGraph.GetType()) || _ignoreTypeNames.Contains(objGraph.GetType().FullName.ToLowerInvariant()) || _ignoreBaseTypes.Contains(objGraph.GetType().BaseType) || TypedPropertyList.IsScalarType(objGraph.GetType()))
            {
                return;
            }

            if (objGraph is MarshalByRefObject)
            {
                _idProp.SetValue(objGraph, null);
            }
            List <PropertyInfo> propList = TypedPropertyList.GetPropertyList(objGraph.GetType(), false, true);

            foreach (PropertyInfo prop in propList)
            {
                if (!prop.PropertyType.Namespace.ToLowerInvariant().StartsWith("system.reflection"))
                {
                    object propValue = prop.GetValue(objGraph);
                    if (propValue != null && !_ignoreTypes.Contains(propValue.GetType()) && !_ignoreTypeNames.Contains(propValue.GetType().FullName.ToLowerInvariant()) && !_ignoreBaseTypes.Contains(propValue.GetType().BaseType) && !TypedPropertyList.IsScalarType(propValue.GetType()))
                    {
                        if (propValue is IEnumerable)
                        {
                            foreach (object propValueX in (IEnumerable)propValue)
                            {
                                SetNullIdentity(propValueX);
                            }
                        }
                        else
                        {
                            SetNullIdentity(propValue);
                        }
                    }
                }
            }
        }
        public static List <T> GetTypedList <T>(ITable table) where T : class, new()
        {
            List <PropertyInfo> propertyList = TypedPropertyList.GetPropertyList(typeof(T));

            List <T> result = new List <T>();

            foreach (Row row in table.Rows)
            {
                T current = new T();
                foreach (Column column in row.Columns)
                {
                    if (!string.IsNullOrWhiteSpace(((Context)column.Context).TargetFieldName))
                    {
                        PropertyInfo targetPropery = propertyList.SingleOrDefault(pi => pi.Name == ((Context)column.Context).TargetFieldName);
                        if (targetPropery != null && targetPropery.CanWrite)
                        {
                            targetPropery.SetValue(current, column.ActualValue, null);
                        }
                    }
                }
                result.Add(current);
            }

            return(result);
        }
        public static List <T> GetTypedListFromTable <T>(Table table) where T : new()
        {
            if (typeof(T) == typeof(CustomDynamicObject) || typeof(T) == typeof(DynamicObject) || typeof(T) == typeof(ExpandoObject))
            {
                throw new InvalidOperationException("Tables based on types CustomDynamicObject, DynamicObject, or ExpandoObject cannot be converted to a typed List.");
            }
            if (table.IsPivoted)
            {
                throw new InvalidOperationException("Pivoted Tables cannot be converted to a typed List.");
            }

            List <PropertyInfo> propertyList = TypedPropertyList.GetPropertyList(typeof(T));

            List <T> resultList = new List <T>();

            foreach (Row currentRow in table.Rows)
            {
                T currentT = new T();
                foreach (Column currentColumn in currentRow.Columns)
                {
                    if (!string.IsNullOrWhiteSpace(currentColumn.Context.PropInfo.Name))
                    {
                        PropertyInfo targetPropery = propertyList.Where(pi => pi.Name == currentColumn.Context.PropInfo.Name).SingleOrDefault();
                        if (targetPropery != null && targetPropery.CanWrite)
                        {
                            targetPropery.SetValue(currentT, currentColumn.ActualValue, null);
                        }
                    }
                }
                resultList.Add(currentT);
            }

            return(resultList);
        }
        public static object GetType(Type t, IRow row)
        {
            List <PropertyInfo> propertyList = TypedPropertyList.GetPropertyList(t);

            //T result = new T();
            object result = Activator.CreateInstance(t);

            foreach (Column column in row.Columns)
            {
                if (!string.IsNullOrWhiteSpace(((Context)column.Context).TargetFieldName))
                {
                    PropertyInfo targetPropery = propertyList.SingleOrDefault(pi => pi.Name == ((Context)column.Context).TargetFieldName);
                    if (targetPropery != null && targetPropery.CanWrite)
                    {
                        targetPropery.SetValue(result, column.ActualValue, null);
                    }
                }
            }

            return(result);
        }
        public static Table GetTableFromTypedList <T>(List <T> typedList) where T : new()
        {
            List <PropertyInfo> propertyList;
            Table resultTable = new Table();

            if (typeof(T) != typeof(CustomDynamicObject))
            {
                propertyList = TypedPropertyList.GetPropertyList(typeof(T));

                foreach (T currentT in typedList)
                {
                    Row currentRow = new Row(resultTable);
                    foreach (PropertyInfo propInfo in propertyList)
                    {
                        Column currentColumn = null;
                        if (!string.IsNullOrWhiteSpace(propInfo.Name))
                        {
                            if (propInfo.CanRead && propInfo.CanWrite)
                            {
                                Context     context = new Context(propInfo);
                                IComparable value   = (IComparable)propInfo.GetValue(currentT, null);
                                currentColumn = new Column(currentRow, value, context);
                            }
                        }
                        if (currentColumn != null)
                        {
                            currentRow.Columns.Add(currentColumn);
                        }
                    }
                    if (currentRow.Columns.Count > 0)
                    {
                        resultTable.Rows.Add(currentRow);
                    }
                }
            }
            else
            {
                if (typedList.Count > 0)
                {
                    Dictionary <string, Type> propertyDict = (typedList[0] as CustomDynamicObject).GetPropertyNamesAndTypes();

                    foreach (T currentT in typedList)
                    {
                        Row currentRow = new Row(resultTable);
                        foreach (KeyValuePair <string, Type> propInfo in propertyDict)
                        {
                            Column currentColumn       = null;
                            CreateablePropertyInfo cpi = new CreateablePropertyInfo(propInfo.Key, propInfo.Value);
                            Context     context        = new Context(cpi.ToPropertyInfo());
                            IComparable value          = (IComparable)((currentT as CustomDynamicObject).GetPropertyValue(propInfo.Key));
                            currentColumn = new Column(currentRow, value, context);

                            if (currentColumn != null)
                            {
                                currentRow.Columns.Add(currentColumn);
                            }
                        }
                        if (currentRow.Columns.Count > 0)
                        {
                            resultTable.Rows.Add(currentRow);
                        }
                    }
                }
            }

            return(resultTable);
        }
示例#6
0
        public void GenerateLine(Tuple <object, List <object> > dataObjects, List <Exception> exceptions)
        {
            bool isFixedWidth  = _fileToGenerate.IsFixedWidth;
            bool isMultiSchema = _fileToGenerate.IsMultiSchema;

            errorType = "File generation error";
            {
                FileType fileType = null;
                if (isMultiSchema)
                {
                    fileType = _fileToGenerate.FileTypes.SingleOrDefault(ft => ft.SchemaIdentifier == (string)dataObjects.Item1);
                }
                else
                {
                    fileType = _fileToGenerate.FileTypes[0];
                }
                if (fileType != null)
                {
                    RowDefinition rowDef = null;
                    try
                    {
                        int lastPos = 0;
                        if (isFixedWidth)
                        {
                            lastPos = 1;
                        }
                        ArrayList            fieldList = new ArrayList();
                        List <RowDefinition> rowDefs   = fileType.RowDefinitions.OrderBy(rd => rd.SourceColumnNumber).ToList();
                        for (var i = 0; i < rowDefs.Count; i++)
                        {
                            rowDef = rowDefs[i];
                            if (isFixedWidth)
                            {
                                if (rowDef.ParseStartPosition > lastPos)
                                {
                                    int len     = rowDef.ParseStartPosition.Value - lastPos;
                                    var padding = new string(' ', len);
                                    fieldList.Add(padding);
                                }
                                lastPos = rowDef.ParseStartPosition.Value + rowDef.ParseLength.Value;
                            }
                            else
                            {
                                if (rowDef.SourceColumnNumber == null)
                                {
                                    continue;
                                }

                                if (rowDef.SourceColumnNumber > (lastPos + 1))
                                {
                                    for (var s = 0; s < rowDef.SourceColumnNumber - 1; s++)
                                    {
                                        fieldList.Add("");
                                    }
                                }
                                lastPos = rowDef.SourceColumnNumber.Value;
                            }
                            object obj = null;
                            if (rowDef.SourceColumnNumber == -1)
                            {
                                string schemaId = ConvertValue(fileType.SchemaIdentifier, rowDef.DataType, rowDef.FieldFormat, isFixedWidth, rowDef.ParseLength);
                                if (schemaId != null)
                                {
                                    fieldList.Add(schemaId);
                                }
                            }
                            else
                            {
                                obj = dataObjects.Item2.SingleOrDefault(o => o.GetType().Name == rowDef.TargetTableName);
                                if (obj == null)
                                {
                                    obj = dataObjects.Item2.SingleOrDefault(o => o.GetType().GetCustomAttribute <ParseTargetAttribute>() != null && o.GetType().GetCustomAttribute <ParseTargetAttribute>().DisplayName == rowDef.TargetTableName);
                                }
                            }
                            if (obj != null)
                            {
                                PropertyInfo pInfo = obj.GetType().GetProperty(rowDef.TargetFieldName);
                                if (pInfo != null)
                                {
                                    string result = null;
                                    object value  = pInfo.GetValue(obj);
                                    if (value == null && rowDef.IsRequired)
                                    {
                                        if (rowDef.CanSetDefault && rowDef.DefaultValue != null)
                                        {
                                            value = rowDef.DefaultValue;
                                        }
                                        if (value == null)
                                        {
                                            throw new Exception("[" + rowDef.FieldDisplayName + "] is a required field but has no value.");
                                        }
                                    }

                                    result = ConvertValue(value, rowDef.DataType, rowDef.FieldFormat, isFixedWidth, rowDef.ParseLength);

                                    if (result != null)
                                    {
                                        fieldList.Add(result);
                                    }
                                    else
                                    {
                                        fieldList.Add("");
                                    }
                                }
                            }
                        }
                        if (!_fileToGenerate.IsMultiSchema && !_fileToGenerate.IsFixedWidth && _fileToGenerate.AppendUnmappedDataToEnd)
                        {
                            CustomDynamicObject cdo = null;
                            foreach (object obj in dataObjects.Item2)
                            {
                                List <PropertyInfo> propList = TypedPropertyList.GetPropertyList(obj.GetType(), false, true);
                                foreach (PropertyInfo prop in propList)
                                {
                                    object propVal = prop.GetValue(obj);
                                    if (propVal is CustomDynamicObject)
                                    {
                                        cdo = propVal as CustomDynamicObject;
                                        break;
                                    }
                                    if (cdo != null)
                                    {
                                        break;
                                    }
                                }
                            }
                            if (cdo != null)
                            {
                                var dictNamesTypes = cdo.GetPropertyNamesAndTypes();
                                foreach (var dictNameType in dictNamesTypes)
                                {
                                    var val = cdo.GetPropertyValue(dictNameType.Key);
                                    fieldList.Add(val != null ? val.ToString() : "");
                                }
                            }
                        }
                        _generator.WriteFields(fieldList.Cast <string>().ToArray());
                    }
                    catch (Exception e)
                    {
                        string columnDesc = string.Empty;
                        if (rowDef != null)
                        {
                            columnDesc = string.Format("Field '{0}', ", rowDef.FieldDisplayName);
                        }

                        exceptions.Add(new Exception("Generation Warning",
                                                     new Exception(string.Format(
                                                                       "Unable to generate line {0}, {1} column {2}.  Error: {3}", _currentLineNumber + 1,
                                                                       columnDesc, rowDef.SourceColumnNumber.Value, e.Message))));
                        if (_fileToGenerate.MaximumParsingErrors > 0 && exceptions.Count >= _fileToGenerate.MaximumParsingErrors)
                        {
                            errorType = string.Empty;
                            throw new Exception(string.Format("Aborting export after {0} Generation Warnings",
                                                              _fileToGenerate.MaximumParsingErrors));
                        }
                    }
                }
                _currentLineNumber++;
            }

            //skip footer lines if defined
            if (_footerLines > 0)
            {
                short skipFooterLines = 0;
                while (skipFooterLines < _footerLines)
                {
                    _generator.WriteLine();
                    skipFooterLines++;
                    _currentLineNumber++;
                }
            }
        }