zoukankan      html  css  js  c++  java
  • 解决序列化类型为“System.Reflection.RuntimeModule”的对象时检测到循环引用。

    定义一个继承JavaScriptConverter的子类

    public class DataTableConverter : JavaScriptConverter
        {
            /// <summary>序列化</summary>
            public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
            {
                DataTable dt = obj as DataTable;
                Dictionary<string, object> result = new Dictionary<string, object>();
    
                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
    
                foreach (DataRow dr in dt.Rows)
                {
                    Dictionary<string, object> row = new Dictionary<string, object>();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        row.Add(dc.ColumnName, dr[dc.ColumnName]);
                    }
                    rows.Add(row);
                }
    
                result["Rows"] = rows;
    
                return result;
            }
    
            /// <summary>反序列化</summary>
            public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
            {
                throw new NotImplementedException();
            }
    
            /// <summary>
            /// 获取本转换器支持的类型
            /// </summary>
            public override IEnumerable<Type> SupportedTypes
            {
                get { return new Type[] { typeof(DataTable) }; }
            }
        }

    序列化之前先将对象进行注册

    JavaScriptSerializer resultjson = new JavaScriptSerializer();
    
    resultjson.RegisterConverters(new JavaScriptConverter[] { new DataTableConverter() });
    
    string data = resultjson.Serialize( this );
  • 相关阅读:
    ES基本介绍
    Mybatis 读写分离简单实现
    分享一个Flink checkpoint失败的问题和解决办法
    一次“内存泄露”引发的血案
    记一次堆外内存泄漏排查过程
    MySQL主从复制读写分离,看这篇就够了!
    JVM运行时内存数据区域
    .NET MVC 页面传值方式
    jQuery 对表格内容进行搜索筛选
    泛型
  • 原文地址:https://www.cnblogs.com/jiyang2008/p/7357634.html
Copyright © 2011-2022 走看看