博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NBear实例讲解
阅读量:5047 次
发布时间:2019-06-12

本文共 6201 字,大约阅读时间需要 20 分钟。

Nbear的接口主要是调用ObjectConvertor类的3大类方法:ToObject,ToList,ToDataTable。

主要功能:不同类型的对象可以通过类型属性的映射,进行转换,包括类与接口。对于两种类型毫无继承关系,且两种类型的数据需要转换时,Nbear是很有用的。

另外Nbear是开源代码,在类型转换的过程中,主要使用 System.Reflection.Emit;反射来取值并赋值到新对象。使用IL编程一定程度上提高了程序的效率。

要研究IL的网友,可以认真研究一下其源码。

数据模型
1   public interface IUser 2     { 3  4         string Name 5         { 6             get; 7             set; 8         } 9 10         int Age11         {12             get;13             set;14         }15 16     }17 18     public class User : IUser19     {20         //需要无参数构造函数21         public User()22         {23         }24         public User(string name, int age, bool isvisible)25         {26             this.Name=name;27             this.Age=age;28             this.IsVisible=isvisible;29         }30 31         public string Name32         {33             get;34             set;35         }36 37         public int Age38         {39             get;40             set;41         }42         public bool IsVisible43         {44             get;45             set;46         }47 48     }
1    using NBear.Mapping;  2     using NBearLiteDemo.Model;  3     class Program  4     {  5         //ObjectConvertor类:: ToObject;ToList;ToDataTable  6         static void Main(string[] args)  7         {  8            9             10             User user=new User("Jefftary", 24, true); 11             User[] users=new User[] { user,new User("Jackbaby",19,true)}; 12            13             User outputObject1 = ObjectConvertor.ToObject
(user); 14 User outputObject2 = ObjectConvertor.ToObject
(user); 15 16 //User-Others 17 DataRow outputObject3 = ObjectConvertor.ToObject
(user); 18 Hashtable outputObject4 = ObjectConvertor.ToObject
(user); 19 NameValueCollection outputObject5 = ObjectConvertor.ToObject
(user); 20 Dictionary
outputObject6 = ObjectConvertor.ToObject
>(user); 21 22 //Others->User 23 User outputObject31 = ObjectConvertor.ToObject
(outputObject3); 24 User outputObject41 = ObjectConvertor.ToObject
(outputObject4); 25 User outputObject51 = ObjectConvertor.ToObject
(outputObject5); 26 User outputObject61 = ObjectConvertor.ToObject
(outputObject5); 27 28 object[] list=new object[] { outputObject1, outputObject2, outputObject3, outputObject4, outputObject5, outputObject6 }; 29 object[] list2=new object[] { outputObject31, outputObject41, outputObject51, outputObject61 }; 30 31 //Array->List
32 List
userList = ObjectConvertor.ToList
(users); 33 34 //Array->DataTable->List
->DataTable 35 DataTable userTable = ObjectConvertor.ToDataTable(users, typeof(User)); 36 List
userList1 = ObjectConvertor.ToList
(userTable); 37 List
userList2 = ObjectConvertor.ToList
(userTable); 38 DataTable userTable2=ObjectConvertor.ToDataTable(userList, typeof(User)); 39 40 41 ShowList(list,"[Object]",true); 42 ShowList(list2, "[Object-Parse]", true); 43 44 ShowList(users, "[Array]"); 45 46 ShowList(userTable, "[ToDataTable(users, typeof(User))]"); 47 ShowList(userList, "[ToList
(users)]"); 48 49 ShowList(userList1, "[ToList
(userTable)]"); 50 ShowList(userList2, "[ToList
(userTable)]"); 51 ShowList(userTable2, "[ToDataTable(userList, typeof(User))]"); 52 53 54 var th=Type.GetTypeHandle(3); 55 56 Console.WriteLine(th); 57 Console.ReadKey(); 58 } 59 60 static void Show(object obj,bool showType=true) 61 { 62 string printFormat="{0}:\r\n\t\t{1}\t{2}\t{3}\r\n"; 63 if(!showType) 64 printFormat="\trow:\t{1}\t{2}\t{3}"; 65 66 Type type=obj.GetType(); 67 if (type.Equals(typeof(User))) 68 { 69 var user=obj as User; 70 Console.WriteLine(string.Format(printFormat, type, user.Name, user.Age, user.IsVisible)); 71 } 72 else 73 { 74 //反射获取索引器 this[Name] 75 var prop = type.GetProperty("Item", new Type[] { typeof(string) }); 76 if (prop!=null) 77 { 78 //DataRow,Hashtable,Dictionary,NameValueCollection 79 var name= prop.GetGetMethod().Invoke(obj, new object[] { "Name" }); 80 var age= prop.GetGetMethod().Invoke(obj, new object[] { "Age" }); 81 var visible= prop.GetGetMethod().Invoke(obj, new object[] { "IsVisible" }); 82 Console.WriteLine("this[Name]:"); 83 Console.WriteLine(string.Format(printFormat, type, name, age, visible)); 84 } 85 } 86 87 //else if (type.Equals(typeof(NameValueCollection))) 88 //{ 89 // var nv=obj as NameValueCollection; 90 91 // Console.WriteLine(string.Format(printFormat, type, nv["Name"], nv["Age"], nv["IsVisible"])); 92 //} 93 //else if (type.Equals(typeof(DataRow))) 94 //{ 95 // var dr=obj as DataRow; 96 // Console.WriteLine(string.Format(printFormat, type, dr["Name"].ToString(), dr["Age"].ToString(), dr["IsVisible"].ToString())); 97 98 //} 99 //else if (typeof(IDataReader).IsAssignableFrom(type))100 //{101 // var rd=obj as IDataReader;102 // Console.WriteLine(string.Format(printFormat, type, rd["Name"].ToString(), rd["Age"].ToString(), rd["IsVisible"].ToString()));103 104 //}105 //else if (type.Equals(typeof(Dictionary
)))106 //{107 // var dc=obj as Dictionary
;108 // Console.WriteLine(string.Format(printFormat, type, dc["Name"].ToString(), dc["Age"].ToString(), dc["IsVisible"].ToString()));109 //}110 //else if (type.Equals(typeof(Hashtable)))111 //{112 // var ht=obj as Hashtable;113 // Console.WriteLine(string.Format(printFormat, type, ht["Name"].ToString(), ht["Age"].ToString(), ht["IsVisible"].ToString()));114 115 //}116 }117 118 static void ShowList(object obj,string title="",bool showType=false)119 {120 Console.WriteLine(string.Format("----------------------------{0}------------------------",title));121 Type type=obj.GetType();122 if (type.Equals(typeof(DataTable)))123 {124 var tb=obj as DataTable;125 //for (int i=0; i
).MakeGenericType(typeof(object)).IsAssignableFrom(type))136 {137 var list=obj as IEnumerable
;138 foreach (var item in list)139 {140 Show(item, showType);141 }142 }143 else if (type.IsArray)144 {145 var ary=obj as Array;146 foreach (var item in ary)147 {148 Show(item,showType);149 }150 }151 }152 153 154 }

 

转载于:https://www.cnblogs.com/AspDotNetMVC/archive/2013/03/16/2943035.html

你可能感兴趣的文章
Beta阶段总结
查看>>
嵌入式Linux开发环境搭建,问题ping、nfs的解决
查看>>
iOS之设置头像(访问系统相册、本地上传)
查看>>
iOS之限制TextField的输入长度
查看>>
webapi 安全验证与权限验证
查看>>
tomcat下部署多个项目
查看>>
Docker-Compose
查看>>
Python list知识内容
查看>>
C - A Plug for UNIX - poj 1087(最大流)
查看>>
UNICODE与ANSI的区别
查看>>
代码备份2
查看>>
工作流基本特性及说明
查看>>
高手给菜鸟学习Linux的10个建议
查看>>
洗牌算法Fisher_Yates原理
查看>>
functools 之 partial(偏函数)
查看>>
多线程2--毕向东基础视频教程学习笔记
查看>>
结对第二次作业
查看>>
jQuery 1.7的隐藏改动
查看>>
初学ant
查看>>
bzoj 4295 [PA2015]Hazard 贪心,暴力
查看>>