NBear实例讲解

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 Age

11         {

12             get;

13             set;

14         }

15 

16     }

17 

18     public class User : IUser

19     {

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 Name

32         {

33             get;

34             set;

35         }

36 

37         public int Age

38         {

39             get;

40             set;

41         }

42         public bool IsVisible

43         {

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>(user);            

 14             User outputObject2 = ObjectConvertor.ToObject<IUser, User>(user);   

 15           

 16             //User-Others

 17             DataRow outputObject3 = ObjectConvertor.ToObject<DataRow>(user);                   

 18             Hashtable outputObject4 = ObjectConvertor.ToObject<Hashtable>(user);                

 19             NameValueCollection outputObject5 = ObjectConvertor.ToObject<NameValueCollection>(user);                

 20             Dictionary<string, object> outputObject6 = ObjectConvertor.ToObject<Dictionary<string, object>>(user);               

 21               

 22             //Others->User

 23             User outputObject31 = ObjectConvertor.ToObject<User>(outputObject3);

 24             User outputObject41 = ObjectConvertor.ToObject<User>(outputObject4);

 25             User outputObject51 = ObjectConvertor.ToObject<User>(outputObject5);     

 26             User outputObject61 = ObjectConvertor.ToObject<User>(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<User>

 32             List<User> userList = ObjectConvertor.ToList<IUser, User>(users);

 33 

 34              //Array->DataTable->List<User>->DataTable

 35             DataTable userTable = ObjectConvertor.ToDataTable(users, typeof(User));

 36             List<User> userList1 = ObjectConvertor.ToList<DataRow, User>(userTable);

 37             List<User> userList2 = ObjectConvertor.ToList<IDataReader, User>(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<IUser, User>(users)]");

 48            

 49             ShowList(userList1, "[ToList<DataRow, User>(userTable)]");

 50             ShowList(userList2, "[ToList<IDataReader, User>(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<string, object>)))

106             //{

107             //    var dc=obj as Dictionary<string, object>;

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<tb.Rows.Count; i++)

126                 //    Show(tb.Rows[i],showType);

127                 IDataReader reader=tb.CreateDataReader();

128                 while (reader.Read())

129                 {

130                     Show(reader, showType);

131                 }            

132 

133 

134             }

135             else if(type.IsGenericType && typeof(IEnumerable<>).MakeGenericType(typeof(object)).IsAssignableFrom(type))

136             {

137                 var list=obj as IEnumerable<object>;

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     }

 

你可能感兴趣的:(实例)