/// <summary> /// Converts any collection of records to a DataTable using reflection. /// WARNING: this methods returns null if the number of records is 0, /// pass the Type of the records to get an empty DataTable. /// </summary> /// <param name="records">The records to be converted to a DataTable</param> /// <param name="maxRecords">The max number of records to add to the DataTable. -1 for all.</param> /// <returns>The DataTable containing the records as DataRows</returns> public static DataTable RecordsToDataTable(ICollection records, int maxRecords) { IRecordInfo ri = null; foreach (var obj in records) { if (obj != null) { ri = RecordInfo.Resolve(obj.GetType()); break; } } if (ri == null) { return(new DataTable()); } return(ri.Operations.RecordsToDataTable(records, maxRecords)); }
/// <summary> /// Create and engine on type with specified encoding /// </summary> /// <param name="recordType">Class to base engine on</param> /// <param name="encoding">encoding of the file</param> internal EngineBase(Type recordType, Encoding encoding) { if (recordType == null) { throw new BadUsageException(Messages.Errors.NullRecordClass.Text); } if (recordType.IsValueType) { throw new BadUsageException(Messages.Errors.StructRecordClass .RecordType(recordType.Name) .Text); } mRecordType = recordType; mRecordInfo = RecordInfo.Resolve(recordType); // Container.Resolve<IRecordInfo>(recordType); mEncoding = encoding; CreateRecordOptions(); }
/// <summary>Create a new instance of the MultiRecordEngine</summary> /// <param name="recordTypes">The Types of the records that this engine can handle.</param> /// <param name="recordSelector"> /// Used only in read operations. The selector indicates to the engine /// what Type to use in each read line. /// </param> public MultiRecordEngine(RecordTypeSelector recordSelector, params Type[] recordTypes) : base(GetFirstType(recordTypes)) { mTypes = recordTypes; mMultiRecordInfo = new IRecordInfo[mTypes.Length]; mRecordInfoHash = new Hashtable(mTypes.Length); for (int i = 0; i < mTypes.Length; i++) { if (mTypes[i] == null) { throw new BadUsageException("The type at index " + i.ToString() + " is null."); } if (mRecordInfoHash.Contains(mTypes[i])) { throw new BadUsageException("The type '" + mTypes[i].Name + " is already in the engine. You can't pass the same type twice to the constructor."); } mMultiRecordInfo[i] = RecordInfo.Resolve(mTypes[i]); mRecordInfoHash.Add(mTypes[i], mMultiRecordInfo[i]); } mRecordSelector = recordSelector; }
/// <summary> /// Converts any collection of records to a DataTable using reflection. /// If the number of records is 0 this methods returns an empty /// DataTable with the columns based on the fields of the /// Type. /// </summary> /// <param name="records">The records to be converted to a DataTable</param> /// <returns>The DataTable containing the records as DataRows</returns> /// <param name="maxRecords">The max number of records to add to the DataTable. -1 for all.</param> /// <param name="recordType">The type of the inner records.</param> public static DataTable RecordsToDataTable(ICollection records, Type recordType, int maxRecords) { IRecordInfo ri = RecordInfo.Resolve(recordType); return(ri.Operations.RecordsToDataTable(records, maxRecords)); }
/// <summary> /// Generic extension method for arrays that returns the array records /// in a DataTable. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="records">The array to transform into a DataTable</param> /// <returns>The array records in a DataTable.</returns> public static DataTable ToDataTable <T>(this T[] records) { var ri = RecordInfo.Resolve(typeof(T)); return(ri.Operations.RecordsToDataTable(records)); }