posts - 21,comments - 64,views - 25015

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

 

要引用命名空间 System.Collections.ObjectModel;

 

DataRowConverter

 

复制代码
代码

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;
        }
    }
}


复制代码

 

 

DataTableConverter

 

复制代码
代码

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;
        }
    }
}


复制代码

 

 

DataSetConverter

 

复制代码
代码

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;
        }
    }
}


 
复制代码

 

 

posted on   王庭安  阅读(422)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
< 2010年4月 >
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示