9using System.Collections;
10using System.Collections.Generic;
12using System.ComponentModel;
13using System.Reflection;
35 public void Fill(DataSet ds,
object source)
37 string className = source.GetType().Name;
38 Fill(ds, className, source);
51 public void Fill(DataSet ds,
string tableName,
object source)
56 dt = ds.Tables[tableName];
57 exists = (dt !=
null);
60 dt =
new DataTable(tableName);
73 public void Fill(DataTable dt,
object source)
79 List<string> columns = GetColumns(source);
80 if (columns.Count < 1)
return;
83 foreach (
string column
in columns)
84 if (!dt.Columns.Contains(column))
85 dt.Columns.Add(column);
88 CopyData(dt, GetIList(source), columns);
93 private IList GetIList(
object source)
95 if (source is IListSource)
96 return ((IListSource)source).GetList();
97 else if (source is IList)
98 return source as IList;
102 ArrayList col =
new ArrayList();
108 [System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1031:DoNotCatchGeneralExceptionTypes")]
109 private void CopyData(
110 DataTable dt, IList ds, List<string> columns)
114 for (
int index = 0; index < ds.Count; index++)
116 DataRow dr = dt.NewRow();
117 foreach (
string column
in columns)
121 dr[column] = GetField(ds[index], column);
125 dr[column] = ex.Message;
137 private List<string> GetColumns(
object source)
142 IListSource iListSource = source as IListSource;
143 if (iListSource !=
null)
144 innerSource = iListSource.GetList();
146 innerSource = source;
148 DataView dataView = innerSource as DataView;
149 if (dataView !=
null)
150 result = ScanDataView(dataView);
154 IEnumerable iEnumerable = innerSource as IEnumerable;
155 if (iEnumerable !=
null)
157 Type childType = Utilities.GetChildItemType(
158 innerSource.GetType());
159 result = ScanObject(childType);
164 result = ScanObject(innerSource.GetType());
170 private List<string> ScanDataView(DataView ds)
172 List<string> result =
new List<string>();
173 for (
int field = 0; field < ds.Table.Columns.Count; field++)
174 result.Add(ds.Table.Columns[field].ColumnName);
178 private List<string> ScanObject(Type sourceType)
180 List<string> result =
new List<string>();
182 if (sourceType !=
null)
185 PropertyInfo[] props = sourceType.GetProperties();
186 if (props.Length >= 0)
187 for (
int column = 0; column < props.Length; column++)
188 if (props[column].CanRead)
189 result.Add(props[column].Name);
192 FieldInfo[] fields = sourceType.GetFields();
193 if (fields.Length >= 0)
194 for (
int column = 0; column < fields.Length; column++)
195 result.Add(fields[column].Name);
204 private static string GetField(
object obj,
string fieldName)
207 DataRowView dataRowView = obj as DataRowView;
208 if (dataRowView !=
null)
211 result = dataRowView[fieldName].ToString();
213 else if (obj is ValueType && obj.GetType().IsPrimitive)
216 result = obj.ToString();
220 string tmp = obj as string;
224 result = (string)obj;
231 Type sourceType = obj.GetType();
234 PropertyInfo prop = sourceType.GetProperty(fieldName);
236 if ((prop ==
null) || (!prop.CanRead))
240 FieldInfo field = sourceType.GetField(fieldName);
244 throw new DataException(
251 result = field.GetValue(obj).ToString();
257 result = prop.GetValue(obj,
null).ToString();
262 throw new DataException(
264 " " + fieldName, ex);
An ObjectAdapter is used to convert data in an object or collection into a DataTable.
void Fill(DataSet ds, object source)
Fills the DataSet with data from an object or collection.
void Fill(DataTable dt, object source)
Fills a DataTable with data values from an object or collection.
void Fill(DataSet ds, string tableName, object source)
Fills the DataSet with data from an object or collection.
A strongly-typed resource class, for looking up localized strings, etc.
static string ErrorReadingValueException
Looks up a localized string similar to Error reading value:.
static string NoSuchValueExistsException
Looks up a localized string similar to No such value exists:.
static string NothingNotValid
Looks up a localized string similar to Argument must not be Nothing.