从Microsoft.Web.Preview.dll中反射得到的代码,之前为了实现对DataTable对象的序列化引用了这个DLL,但是发现这个DLL还是比较大的,就为了用这三个类而引用这个DLL觉得没必要,干脆直接把这三个类拿出来算了。这样就可以丢掉这个DLL了。
要引用命名空间 System.Collections.ObjectModel;

public class DataRowConverter : JavaScriptConverter
{
// Fields
private ReadOnlyCollection<Type> _supportedTypes = new ReadOnlyCollection<Type>(new Type[] { typeof(DataRow) });
// Methods
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotSupportedException();
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
DataRow row = obj as DataRow;
if (row == null)
{
throw new ArgumentException("ArgumentShouldBeDataRow", "obj");
}
Dictionary<string, object> dictionary = new Dictionary<string, object>((row.ItemArray == null) ? 0 : row.ItemArray.Length);
foreach (DataColumn column in row.Table.Columns)
{
dictionary[column.ColumnName] = row[column];
}
return dictionary;
}
// Properties
public override IEnumerable<Type> SupportedTypes
{
get
{
return this._supportedTypes;
}
}
}

public class DataTableConverter : JavaScriptConverter
{
// Fields
private ReadOnlyCollection<Type> _supportedTypes = new ReadOnlyCollection<Type>(new Type[] { typeof(DataTable) });
// Methods
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotSupportedException();
}
protected virtual string GetClientTypeNameForType(Type type)
{
if (typeof(string).IsAssignableFrom(type) || typeof(char).IsAssignableFrom(type))
{
return "String";
}
if (typeof(bool).IsAssignableFrom(type))
{
return "Boolean";
}
if (typeof(DateTime).IsAssignableFrom(type))
{
return "Date";
}
if (((!typeof(int).IsAssignableFrom(type) && !typeof(double).IsAssignableFrom(type)) && (!typeof(float).IsAssignableFrom(type) && !typeof(long).IsAssignableFrom(type))) && (((!typeof(short).IsAssignableFrom(type) && !typeof(byte).IsAssignableFrom(type)) && (!typeof(uint).IsAssignableFrom(type) && !typeof(ulong).IsAssignableFrom(type))) && (!typeof(ushort).IsAssignableFrom(type) && !typeof(sbyte).IsAssignableFrom(type))))
{
return "Object";
}
return "Number";
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
DataTable table = obj as DataTable;
if (table == null)
{
throw new ArgumentException("ArgumentShouldBeDataTable", "obj");
}
IDictionary<string, object> dictionary = new Dictionary<string, object>(2);
if (table.Columns.Count > 0)
{
IDictionary<string, object>[] dictionaryArray = new Dictionary<string, object>[table.Columns.Count];
DataColumn[] primaryKey = table.PrimaryKey;
int index = 0;
foreach (DataColumn column in primaryKey)
{
dictionaryArray[index] = this.SerializeDataColumn(column, true, serializer);
index++;
}
for (int i = 0; index < dictionaryArray.Length; i++)
{
DataColumn column2 = table.Columns[i];
if (Array.IndexOf<DataColumn>(primaryKey, column2) == -1)
{
dictionaryArray[index] = this.SerializeDataColumn(column2, false, serializer);
index++;
}
}
dictionary["columns"] = dictionaryArray;
}
else
{
dictionary["columns"] = null;
}
if (table.Rows.Count > 0)
{
DataRow[] rowArray = new DataRow[table.Rows.Count];
for (int j = 0; j < rowArray.Length; j++)
{
rowArray[j] = table.Rows[j];
}
dictionary["rows"] = rowArray;
return dictionary;
}
dictionary["rows"] = null;
return dictionary;
}
protected virtual IDictionary<string, object> SerializeDataColumn(DataColumn column, bool isPrimaryKeyColumn, JavaScriptSerializer serializer)
{
IDictionary<string, object> dictionary = new Dictionary<string, object>(5);
dictionary["name"] = column.ColumnName;
dictionary["dataType"] = this.GetClientTypeNameForType(column.DataType);
dictionary["defaultValue"] = (column.DefaultValue == DBNull.Value) ? null : column.DefaultValue;
dictionary["readOnly"] = column.ReadOnly;
dictionary["isKey"] = isPrimaryKeyColumn;
return dictionary;
}
// Properties
public override IEnumerable<Type> SupportedTypes
{
get
{
return this._supportedTypes;
}
}
}

public class DataSetConverter : JavaScriptConverter
{
// Fields
private ReadOnlyCollection<Type> _supportedTypes = new ReadOnlyCollection<Type>(new Type[] { typeof(DataSet) });
// Methods
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotSupportedException();
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
DataSet set = obj as DataSet;
if (set == null)
{
throw new ArgumentException("ArgumentShouldBeDataSet", "obj");
}
IDictionary<string, object> dictionary = new Dictionary<string, object>();
DataTable[] tableArray = new DataTable[set.Tables.Count];
for (int i = 0; i < tableArray.Length; i++)
{
tableArray[i] = set.Tables[i];
}
dictionary["tables"] = tableArray;
return dictionary;
}
// Properties
public override IEnumerable<Type> SupportedTypes
{
get
{
return this._supportedTypes;
}
}
}