解决序列化类型为“System.Reflection.RuntimeModule”的对象时检测到循环引用。

定义一个继承JavaScriptConverter的子类

public class DataTableConverter : JavaScriptConverter
    {
        /// 序列化
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            DataTable dt = obj as DataTable;
            Dictionary<string, object> result = new Dictionary<string, object>();

            Liststring, object>> rows = new Liststring, 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;
        }

        /// 反序列化
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }

        /// 
        /// 获取本转换器支持的类型
        /// 
        public override IEnumerable SupportedTypes
        {
            get { return new Type[] { typeof(DataTable) }; }
        }
    }

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

JavaScriptSerializer resultjson = new JavaScriptSerializer();

resultjson.RegisterConverters(new JavaScriptConverter[] { new DataTableConverter() });

string data = resultjson.Serialize( this );

 

转载于:https://www.cnblogs.com/jiyang2008/p/7357634.html

你可能感兴趣的:(解决序列化类型为“System.Reflection.RuntimeModule”的对象时检测到循环引用。)