如果Object是Dictionary类型,直接返回

如果Object是NameValueCollection类型,则添加到Dictionary里

如果Object是Hashtable类型,添加到Dictionary里

...其他键值类型请自己完善

如果Object非上述类型,则用IL语言将其转换成Dictionary类型,

具体代码如下:

  public static Dictionary<string, object> ConvertFromObject(object obj)
{
if (obj is Dictionary<string, object>)
return (Dictionary<string, object>)obj; var nvc = obj as NameValueCollection;
if (nvc != null)
{
Dictionary<string, object> dict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
foreach (string key in nvc.Keys)
dict.Add( key, nvc[key]);
return dict;
} var hs = obj as Hashtable;
if (hs != null)
{
Dictionary<string, object> dict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
foreach (string key in hs.Keys.OfType<string>())
dict.Add(key, hs[key]);
return dict;
}
var type = obj.GetType();
lock (dictionaryCache)
{
Func<object, Dictionary<string, object>> getter;
if (dictionaryCache.TryGetValue(type, out getter) == false)
{
getter = CreateDictionaryGenerator(type);
dictionaryCache[type] = getter;
}
var dict = getter(obj);
return dict;
}
}
        private static Func<object, Dictionary<string, object>> CreateDictionaryGenerator(Type type)
{
var dm = new DynamicMethod((string.Format("Dictionary{0}", Guid.NewGuid())), typeof(Dictionary<string, object>), new[] { typeof(object) }, type, true);
ILGenerator il = dm.GetILGenerator();
il.DeclareLocal(typeof(Dictionary<string, object>));
il.Emit(OpCodes.Nop);
//Dictionary<string, object> dic = new Dictionary<string, object>();
il.Emit(OpCodes.Newobj, typeof(Dictionary<string, object>).GetConstructor(Type.EmptyTypes));
il.Emit(OpCodes.Stloc_0); foreach (var item in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
string columnName = item.Name;
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ldstr, columnName);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, item.GetGetMethod());
if (item.PropertyType.IsValueType)
{
il.Emit(OpCodes.Box, item.PropertyType);
}
il.Emit(OpCodes.Callvirt, typeof(Dictionary<string, object>).GetMethod("Add"));
}
il.Emit(OpCodes.Nop); il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ret);
var func = (Func<object, Dictionary<string, object>>)dm.CreateDelegate(typeof(Func<object, Dictionary<string, object>>));
return func;
}

将Object转换成Dictionary方法的更多相关文章

  1. 用于把List<Object>转换成Map<String,Object>形式

    /** * 用于把List<Object>转换成Map<String,Object>形式,便于存入缓存 * @author zhang_bo * @param keyName ...

  2. java Object转换成指定的类型

    java Object转换成指定的类型 /** * Object转成指定的类型 * @param obj * @param type * @param <T> * @return */ p ...

  3. XML转换成数组方法

    <?php function xmlToArray2($xml) { // 将XML转为array $array_data = json_decode(json_encode(simplexml ...

  4. Spring MVC 后端获取前端提交的json格式字符串并直接转换成control方法对应的参数对象

    场景: 在web应用开发中,spring mvc凭借出现的性能和良好的可扩展性,导致使用日渐增多,成为事实标准,在日常的开发过程中,有一个很常见的场景:即前端通过ajax提交方式,提交参数为一个jso ...

  5. 将object转换成dyamic类型 解决long输出到浏览器过长精度丢失问题

    需求: 数据库使用飘雪算法保存唯一标识  是一个18位长整形 将数据输出到浏览器时出现了精度丢失问题,这是一个重大的BUG.如果没解决好整个项目都要改一遍. 讨论有三个办法 1.把所有实体 数据模型的 ...

  6. C#将Word转换成PDF方法总结(基于Office和WPS两种方案)

    有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片.一个比较好的办法就是将word转换成pdf,然 ...

  7. 将[object Object]转换成json对象

    这两天在做中英文双版的文件,页面根据语言读取不同的内容.js模板用的是ejs json文件: "components":{ "pages":{ "ho ...

  8. java中Object转换成int或String类型方法

    转载: http://www.cnblogs.com/1020182600HENG/p/6137206.html Object obj = getObject(); if(obj instanceof ...

  9. C#如何把List of Object转换成List of T具体类型

    上周码程序的时候碰到个问题,因为设计上的约束,一个方法接受的参数只能为List<object>类型,然而该方法需要处理的真实数据则是确定的List<Currency>.然而C# ...

随机推荐

  1. Add GNOME to a CentOS Minimal Install

    by Jeff Hunter, Sr. Database Administrator Contents Introduction CentOS 6 About the Author Introduct ...

  2. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

  3. python BaseManager分布式学习

    如果我们已经有一个通过Queue通信的多进程程序在同一台机器上运行,现在,由于处理任务的进程任务繁重,希望把发送任务的进程和处理任务的进程分布到两台机器上.怎么用分布式进程实现?原有的Queue可以继 ...

  4. Win32编程点滴3 - 简单ActiveX控件的使用

    虽然这里一片的.net气氛,到处充斥着像MVC.WPF.WorkFlow.LINQ等各种niubility的术语.但我们使用的Windows还是由COM技术主宰着:我们在选择日常使用的软件时,也会避免 ...

  5. Nuget:Newtonsoft.Json

    ylbtech-Nuget:Newtonsoft.Json 1.返回顶部   2.返回顶部 1,Serialize JSON Product product = new Product(); prod ...

  6. 使用 StoryBoard 制作一个能够删除cell的TableView

    本篇博客方便自己检索使用.资源链接 下面是制作效果图,点击删除按钮,就能将该cell删除: 下面是主要的代码: #define KSUPER_TAG 20000 #define KDEFAU_TAG ...

  7. 开源跨平台声波传输库:Sonic

    简介 [Sonic](https://github.com/linyehui/sonic) 是一个跨平台的声波传输库(iOS & Android),技术上类似于[chirp](http://c ...

  8. python_学习笔记

    1,多态:对不同类的对象使用同样的操作,但使用函数显示地检查类型能够毁掉多态(eg: type,isinstance,issubclass) 封装:多态让用户对于不知道是什么类的对象进行方法调用,而封 ...

  9. Levko and Array

    题意: 有一长度为n的正整数序列,你可以选择K个数字任意改变它,使得$max \{ a(i+1) - a(i) \} $ 最小,求最小值. 解法: 1.$O(n^2log(MAX_A) )$,考虑二分 ...

  10. Matrix Recurrence

    给定矩阵$A,B$,且有 $$f(0) = A ,f(i) =B * \prod_{i=w(i)}^{i-1}f(i)$$ 求f(n) 其中,当w(i)单增时,可以做到$O(n*m^3)$,注意要优化 ...