将DataTable转换为PagedCollectionView数据,我们可以借用DataTable的GetBindableData()方法,如下:

  1. DataTable dt=new DataTable();
  2. PagedCollectionView m_pagedCollectionView = new PagedCollectionView(dt.GetBindableData(new Connector()));
  3. this.daDatas.ItemsSource = m_pagedCollectionView;

问题:如果直接调用GetBindableData方法的话,我们得到的所有PagedCollectionView数据将都是string类型的,为了得到数据类型的数据,我们可以自己写转换方法

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Reflection;
  5. using System.Reflection.Emit;
  6. using System.Threading;
  7. using SuperMap.Web.ISDotNET6;
  8. using WaterWebGIS.MainFrame.BL;
  9. using SL40PropertyGrid;
  10. using System.ComponentModel;
  11. using Silverlight;
  12. using WaterWebGIS.QueryStatServiceProxy;
  13. using DataColumn = Silverlight.DataColumn;
  14. using DataRow = Silverlight.DataRow;
  15. using DataTable = Silverlight.DataTable;
  16. using FieldInfo = WaterWebGIS.QueryStatServiceProxy.FieldInfo;
  17.  
  18. namespace WaterWebGIS.Business.DataModel
  19. {
  20. public class TypeFactory
  21. {
  22. private static List<CategoryPriorityInfo> s_categoryPriorityInfo;
  23. private static Dictionary<string, Type> s_dicTypes;
  24. private static AssemblyBuilder s_asmBuilder;
  25. private static ModuleBuilder s_modBuilder;
  26.  
  27. /// <summary>
  28. /// 获取分类优先级信息
  29. /// </summary>
  30. public static void GetCategoryPriorityInfo()
  31. {
  32. QueryStatServiceSoapClient client = (QueryStatServiceSoapClient)WaterWebGIS.Business.Utility.CreateWebServiceObject(typeof(QueryStatServiceSoapClient), "/WS/QueryService/QueryStatService.asmx");
  33. client.GetCategoryPriorityAsync();
  34. client.GetCategoryPriorityCompleted += new EventHandler<GetCategoryPriorityCompletedEventArgs>(client_GetCategoryPriorityCompleted);
  35. }
  36.  
  37. static void client_GetCategoryPriorityCompleted(object sender, GetCategoryPriorityCompletedEventArgs e)
  38. {
  39. if (e.Error == null)
  40. {
  41. s_categoryPriorityInfo = new List<CategoryPriorityInfo>();
  42. foreach (CategoryPriorityInfo info in e.Result)
  43. {
  44. s_categoryPriorityInfo.Add(info);
  45. }
  46. }
  47. }
  48.  
  49. /// <summary>
  50. /// 将数据库中的基础数据类型转换为.Net框架中的数据类型
  51. /// </summary>
  52. /// <param name="dbType">将数据库中保存的类型信息转换成.net中相应的类型</param>
  53. public static Type ToDotNetTypeFromDBType(string dbType)
  54. {
  55. if (dbType.ToLower() == "varchar" || dbType.ToLower() == "datetime")
  56. {
  57. return typeof(System.String);
  58. }
  59. else if (dbType.ToLower() == "int")
  60. {
  61. return typeof(System.Int32);
  62. }
  63. else if (dbType.ToLower() == "float")
  64. {
  65. return typeof(System.Double);
  66. }
  67.  
  68. return typeof(DBNull);
  69. }
  70.  
  71. /// <summary>
  72. /// 生成应用程序集和模块
  73. /// </summary>
  74. private static void GenerateAssemboyAndModule()
  75. {
  76. if (s_asmBuilder == null)
  77. {
  78. AssemblyName assemblyName = new AssemblyName();
  79. assemblyName.Name = "DynamicORMapper";
  80. AppDomain thisDomain = Thread.GetDomain();
  81. s_asmBuilder = thisDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
  82. s_modBuilder = s_asmBuilder.DefineDynamicModule(assemblyName.Name);
  83. }
  84. }
  85.  
  86. /// <summary>
  87. /// 创建类型
  88. /// </summary>
  89. /// <param name="modBuilder">模块生成器</param>
  90. /// <param name="layer">图层信息</param>
  91. /// <returns>类型信息</returns>
  92. private static Type CreateType(ModuleBuilder modBuilder, WaterWebGIS.QueryStatServiceProxy.GISLayer layer)
  93. {
  94. TypeBuilder typeBuilder = modBuilder.DefineType(layer.LayerNameEN, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout);
  95.  
  96. CreateConstructor(typeBuilder);
  97. CreateProperties(typeBuilder, layer.FieldENlist);
  98.  
  99. return typeBuilder.CreateType();
  100. }
  101.  
  102. /// <summary>
  103. /// 创建类型
  104. /// </summary>
  105. /// <param name="modBuilder">模块生成器</param>
  106. /// <param name="table">DataTable信息</param>
  107. /// <returns>类型信息</returns>
  108. private static Type CreateType(ModuleBuilder modBuilder, DataTable table)
  109. {
  110. string tableName = "Table" + Environment.TickCount;
  111. TypeBuilder typeBuilder = modBuilder.DefineType(tableName, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout);
  112.  
  113. CreateConstructor(typeBuilder);
  114. CreateProperties(typeBuilder, table.Columns);
  115.  
  116. return typeBuilder.CreateType();
  117. }
  118.  
  119. /// <summary>
  120. /// 创建类构造函数
  121. /// </summary>
  122. /// <param name="typeBuilder">类型生成器</param>
  123. private static void CreateConstructor(TypeBuilder typeBuilder)
  124. {
  125. ConstructorBuilder construtor = typeBuilder.DefineConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, new Type[]);
  126. ConstructorInfo conObj = typeof(object).GetConstructor(new Type[]);
  127.  
  128. ILGenerator il = construtor.GetILGenerator();
  129. il.Emit(OpCodes.Ldarg_0);
  130. il.Emit(OpCodes.Call, conObj);
  131. il.Emit(OpCodes.Ret);
  132. }
  133.  
  134. /// <summary>
  135. /// 创建类属性信息
  136. /// </summary>
  137. /// <param name="typeBuilder">类型生成器</param>
  138. /// <param name="fieldInfoCollection">图层字段集合</param>
  139. private static void CreateProperties(TypeBuilder typeBuilder, ObservableCollection<WaterWebGIS.QueryStatServiceProxy.FieldInfo> fieldInfoCollection)
  140. {
  141. foreach (WaterWebGIS.QueryStatServiceProxy.FieldInfo fi in fieldInfoCollection)
  142. {
  143. if (fi.IsVisible == true)
  144. {
  145. FieldBuilder fieldBuilder = typeBuilder.DefineField("m" + fi.FieldENName, ToDotNetTypeFromDBType(fi.FieldType), FieldAttributes.Private);
  146.  
  147. PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(fi.FieldENName, PropertyAttributes.HasDefault, ToDotNetTypeFromDBType(fi.FieldType), null);
  148.  
  149. Type[] ctorParams = new Type[] { typeof(string) };
  150. ConstructorInfo classCtorInfo = typeof(DisplayNameAttribute).GetConstructor(ctorParams);
  151. CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, new object[] { fi.FieldAlias });
  152. propertyBuilder.SetCustomAttribute(customAttributeBuilder);
  153.  
  154. classCtorInfo = typeof(CategoryAttribute).GetConstructor(ctorParams);
  155. customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, new object[] { fi.Category });
  156. propertyBuilder.SetCustomAttribute(customAttributeBuilder);
  157.  
  158. ctorParams = new Type[] { typeof(int) };
  159. classCtorInfo = typeof(CategoryPriorityAttribute).GetConstructor(ctorParams);
  160. customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, new object[] { GetCategoryPriorityValueByCategoryName(fi.Category) });
  161. propertyBuilder.SetCustomAttribute(customAttributeBuilder);
  162.  
  163. MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
  164. MethodBuilder getMethodBuilder = typeBuilder.DefineMethod("get_" + fi.FieldENName, getSetAttr, ToDotNetTypeFromDBType(fi.FieldType), Type.EmptyTypes);
  165. ILGenerator ilGenerator = getMethodBuilder.GetILGenerator();
  166.  
  167. ilGenerator.Emit(OpCodes.Ldarg_0);
  168. ilGenerator.Emit(OpCodes.Ldfld, fieldBuilder);
  169. ilGenerator.Emit(OpCodes.Ret);
  170.  
  171. MethodBuilder setMethodBuilder = typeBuilder.DefineMethod("set_" + fi.FieldENName, getSetAttr, null, new Type[] { ToDotNetTypeFromDBType(fi.FieldType) });
  172. ilGenerator = setMethodBuilder.GetILGenerator();
  173. ilGenerator.Emit(OpCodes.Ldarg_0);
  174. ilGenerator.Emit(OpCodes.Ldarg_1);
  175. ilGenerator.Emit(OpCodes.Stfld, fieldBuilder);
  176. ilGenerator.Emit(OpCodes.Ret);
  177.  
  178. propertyBuilder.SetGetMethod(getMethodBuilder);
  179. propertyBuilder.SetSetMethod(setMethodBuilder);
  180. }
  181. }
  182. }
  183.  
  184. /// <summary>
  185. /// 根据分类名称获取该分类的显示优先级
  186. /// </summary>
  187. /// <param name="categoryName">分类名称</param>
  188. /// <returns>显示优先级</returns>
  189. private static int GetCategoryPriorityValueByCategoryName(string categoryName)
  190. {
  191. if (s_categoryPriorityInfo != null)
  192. {
  193. foreach (CategoryPriorityInfo info in s_categoryPriorityInfo)
  194. {
  195. if (info.CategoryName == categoryName)
  196. {
  197. return info.Priority;
  198. }
  199. }
  200. }
  201.  
  202. return int.MaxValue;
  203. }
  204.  
  205. /// <summary>
  206. /// 创建类属性信息
  207. /// </summary>
  208. /// <param name="typeBuilder">类型生成器</param>
  209. /// <param name="dataColumnCollection">DataColumnCollection</param>
  210. private static void CreateProperties(TypeBuilder typeBuilder, DataColumnCollection dataColumnCollection)
  211. {
  212. foreach (DataColumn dataColumn in dataColumnCollection)
  213. {
  214. FieldBuilder fieldBuilder = null;
  215. if (dataColumn.DataType == typeof(DateTime))
  216. {
  217. fieldBuilder = typeBuilder.DefineField("m_" + dataColumn.ColumnName, typeof(string), FieldAttributes.Private);
  218. }
  219. else
  220. {
  221. fieldBuilder = typeBuilder.DefineField("m_" + dataColumn.ColumnName, typeof(string), FieldAttributes.Private);
  222. }
  223.  
  224. PropertyBuilder propertyBuilder = null;
  225. if (dataColumn.DataType == typeof(DateTime))
  226. {
  227. propertyBuilder = typeBuilder.DefineProperty(dataColumn.ColumnName, PropertyAttributes.HasDefault, typeof(string), null);
  228. }
  229. else
  230. {
  231. propertyBuilder = typeBuilder.DefineProperty(dataColumn.ColumnName, PropertyAttributes.HasDefault, typeof(string), null);
  232. }
  233.  
  234. Type[] ctorParams = new Type[] { typeof(string) };
  235. ConstructorInfo classCtorInfo = typeof(DisplayNameAttribute).GetConstructor(ctorParams);
  236.  
  237. MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
  238. MethodBuilder getMethodBuilder = typeBuilder.DefineMethod("get_" + dataColumn.ColumnName, getSetAttr, typeof(string), Type.EmptyTypes);
  239. ILGenerator ilGenerator = getMethodBuilder.GetILGenerator();
  240.  
  241. ilGenerator.Emit(OpCodes.Ldarg_0);
  242. ilGenerator.Emit(OpCodes.Ldfld, fieldBuilder);
  243. ilGenerator.Emit(OpCodes.Ret);
  244.  
  245. MethodBuilder setMethodBuilder = typeBuilder.DefineMethod("set_" + dataColumn.ColumnName, getSetAttr, null, new Type[] { typeof(string) });
  246. ilGenerator = setMethodBuilder.GetILGenerator();
  247. ilGenerator.Emit(OpCodes.Ldarg_0);
  248. ilGenerator.Emit(OpCodes.Ldarg_1);
  249. ilGenerator.Emit(OpCodes.Stfld, fieldBuilder);
  250. ilGenerator.Emit(OpCodes.Ret);
  251.  
  252. propertyBuilder.SetGetMethod(getMethodBuilder);
  253. propertyBuilder.SetSetMethod(setMethodBuilder);
  254. }
  255. }
  256.  
  257. /// <summary>
  258. /// 根据类名获取相应的类信息
  259. /// </summary>
  260. /// <param name="typeName">类名</param>
  261. /// <returns>类</returns>
  262. public static Type GetTypeByTypeName(string typeName)
  263. {
  264. try
  265. {
  266. if (s_dicTypes == null)
  267. {
  268. if (s_asmBuilder == null)
  269. {
  270. GenerateAssemboyAndModule();
  271. }
  272.  
  273. s_dicTypes = new Dictionary<string, Type>();
  274.  
  275. foreach (WaterWebGIS.QueryStatServiceProxy.GISLayer gisLayer in BasicGISService.GetLayers().LayerList)
  276. {
  277. try
  278. {
  279. Type type = CreateType(s_modBuilder, gisLayer);
  280. s_dicTypes.Add(gisLayer.LayerNameEN, type);
  281. }
  282. catch
  283. {
  284. }
  285.  
  286. }
  287. }
  288.  
  289. if (s_dicTypes.ContainsKey(typeName))
  290. {
  291. return s_dicTypes[typeName];
  292. }
  293. else
  294. {
  295. return null;
  296. }
  297. }
  298. catch
  299. {
  300. return null;
  301. }
  302. }
  303.  
  304. public static object CreateObjectBaseOnLayerInfoAndObjectValue(WaterWebGIS.QueryStatServiceProxy.GISLayer gisLayer, SelectAction objectValue)
  305. {
  306. try
  307. {
  308. Type type = GetTypeByTypeName(gisLayer.LayerNameEN);
  309. if (type != null)
  310. {
  311. object obj = Activator.CreateInstance(type);
  312. if (obj != null)
  313. {
  314. foreach (WaterWebGIS.QueryStatServiceProxy.FieldInfo fi in gisLayer.FieldENlist)
  315. {
  316. string fieldValue = BasicGISService.GetLayers().GetValueFromRecord(objectValue.RecordSet, objectValue.Record, fi.FieldENName);
  317. object value = ConvertStringValueToSpecifyTypeValue(fieldValue, fi.FieldType);
  318.  
  319. if (fi.FieldType == "datetime")
  320. {
  321. if (((DateTime)value) == DateTime.MinValue)
  322. {
  323. value = string.Empty;
  324. }
  325. else
  326. {
  327. value = value.ToString();
  328. }
  329. }
  330. if (fi.FieldType == "float")
  331. {
  332. value = Math.Round(Convert.ToDouble(value), );
  333. }
  334.  
  335. PropertyInfo propertyInfo = type.GetProperty(fi.FieldENName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty);
  336. if (propertyInfo != null)
  337. {
  338. propertyInfo.SetValue(obj, value, null);
  339. }
  340. }
  341. }
  342.  
  343. return obj;
  344. }
  345.  
  346. return null;
  347. }
  348. catch
  349. {
  350. return null;
  351. }
  352. }
  353.  
  354. /// <summary>
  355. /// 将字符串值转换成指定类型的值
  356. /// </summary>
  357. /// <returns>转换后的对象值</returns>
  358. private static object ConvertStringValueToSpecifyTypeValue(string value, string dbType)
  359. {
  360. if (dbType.ToLower() == "varchar")
  361. {
  362. return value;
  363. }
  364. else if (dbType.ToLower() == "int")
  365. {
  366. int temp = ;
  367. int.TryParse(value, out temp);
  368. return temp;
  369. }
  370. else if (dbType.ToLower() == "float")
  371. {
  372. float temp = ;
  373. float.TryParse(value, out temp);
  374. return temp;
  375. }
  376. else if (dbType.ToLower() == "datetime")
  377. {
  378. DateTime datetime;
  379. if (DateTime.TryParse(value, out datetime))
  380. {
  381. if (value != "0:00:00")
  382. {
  383. return datetime;
  384. }
  385. else
  386. {
  387. DateTime superMapDate = new DateTime(, , , , , );
  388. return superMapDate;
  389. }
  390. }
  391. else
  392. {
  393. return new DateTime();
  394. }
  395. }
  396.  
  397. return null;
  398. }
  399.  
  400. public static Type CreateTypeBaseOnDataTable(DataTable table)
  401. {
  402. if (s_asmBuilder == null)
  403. {
  404. GenerateAssemboyAndModule();
  405. }
  406.  
  407. return CreateType(s_modBuilder, table);
  408. }
  409.  
  410. /// <summary>
  411. /// 根据DataTable返回相应的对象集合
  412. /// </summary>
  413. /// <param name="table">DataTable</param>
  414. /// <returns>对象集合</returns>
  415. public static List<object> CreateObjectCollectionBaseOnDataTable(DataTable table)
  416. {
  417. if (table == null)
  418. {
  419. throw new ArgumentNullException("table不能为空!");
  420. }
  421.  
  422. List<object> collection = new List<object>();
  423.  
  424. try
  425. {
  426. Type type = CreateTypeBaseOnDataTable(table);
  427.  
  428. if (type != null)
  429. {
  430. foreach (DataRow row in table.Rows)
  431. {
  432. object obj = Activator.CreateInstance(type);
  433. if (obj != null)
  434. {
  435. foreach (DataColumn column in table.Columns)
  436. {
  437. PropertyInfo propertyInfo = type.GetProperty(column.ColumnName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty);
  438. if (propertyInfo != null)
  439. {
  440. if (!string.IsNullOrEmpty(row[column.ColumnName]))
  441. {
  442. propertyInfo.SetValue(obj, ConvertStringToSpecifyType(column.DataType.FullName, row[column.ColumnName]), null);
  443. }
  444. }
  445. }
  446. }
  447.  
  448. collection.Add(obj);
  449. }
  450. }
  451.  
  452. return collection;
  453. }
  454. catch
  455. {
  456. return collection;
  457. }
  458. }
  459.  
  460. private static object ConvertStringToSpecifyType(string typeName, string value)
  461. {
  462. switch (typeName)
  463. {
  464. case "System.Int32":
  465. //return int.Parse(value);
  466. return value;
  467. case "System.DateTime":
  468. DateTime? temp = DateTime.Parse(value);
  469. return temp.Value.ToString();
  470. case "System.Double":
  471. //return double.Parse(value);
  472. return value;
  473. case "System.Decimal":
  474. //return Decimal.Parse(value);
  475. return value;
  476. case "System.Byte":
  477. //return byte.Parse(value);
  478. return value;
  479. case "System.String":
  480. default:
  481. return value;
  482. }
  483. }
  484.  
  485. #region 除指定列转换为string类型,其他类型按所给的数据类型进行转换
  486. //add by qzl @hn at20140424
  487. /// <summary>
  488. /// 除指定列转换为string类型,其他类型按所给的数据类型进行转换
  489. /// </summary>
  490. /// <param name="gisLayer">图层</param>
  491. /// <param name="objectValue">数据集</param>
  492. /// <param name="lstFiledName">转换为string类型的列</param>
  493. /// <returns></returns>
  494. public static object CreateObjectBaseOnLayerInfoAndObjectValueAndChangeText(GISLayer gisLayer, SelectAction objectValue, List<string> lstFiledName)
  495. {
  496. try
  497. {
  498. Type type = GetTypeByTypeNameToString(gisLayer.LayerNameEN, lstFiledName);
  499. if (type != null)
  500. {
  501. object obj = Activator.CreateInstance(type);
  502. if (obj != null)
  503. {
  504. foreach (FieldInfo fi in gisLayer.FieldENlist)
  505. {
  506. object value;
  507. if (lstFiledName.Contains(fi.FieldENName.ToLower()))
  508. {
  509. value ="******";
  510. }
  511. else
  512. {
  513. string fieldValue = BasicGISService.GetLayers().GetValueFromRecord(objectValue.RecordSet, objectValue.Record, fi.FieldENName);
  514. value = ConvertStringValueToSpecifyTypeValue(fieldValue, fi.FieldType);
  515. if (fi.FieldType == "datetime")
  516. {
  517. value = ((DateTime)value) == DateTime.MinValue ? string.Empty : value.ToString();
  518. }
  519. if (fi.FieldType == "float")
  520. {
  521. value = Math.Round(Convert.ToDouble(value), );
  522. }
  523. }
  524. PropertyInfo propertyInfo = type.GetProperty(fi.FieldENName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty);
  525. if (propertyInfo != null)
  526. {
  527. propertyInfo.SetValue(obj, value, null);
  528. }
  529. }
  530. }
  531.  
  532. return obj;
  533. }
  534.  
  535. return null;
  536. }
  537. catch
  538. {
  539. return null;
  540. }
  541. }
  542.  
  543. /// <summary>
  544. /// 根据类名获取相应的类信息
  545. /// </summary>
  546. /// <param name="typeName">类名</param>
  547. /// <returns>类</returns>
  548. public static Type GetTypeByTypeNameToString(string typeName, List<string> lstFiledName)
  549. {
  550. try
  551. {
  552. if (s_dicTypes == null)
  553. {
  554. if (s_asmBuilder == null)
  555. {
  556. GenerateAssemboyAndModule();
  557. }
  558. s_dicTypes = new Dictionary<string, Type>();
  559.  
  560. foreach (GISLayer gisLayer in BasicGISService.GetLayers().LayerList)
  561. {
  562. try
  563. {
  564. Type type = CreateTypeToString(s_modBuilder, gisLayer, lstFiledName);
  565. s_dicTypes.Add(gisLayer.LayerNameEN, type);
  566. }
  567. catch
  568. {
  569. }
  570. }
  571. }
  572.  
  573. if (s_dicTypes.ContainsKey(typeName))
  574. {
  575. return s_dicTypes[typeName];
  576. }
  577. return null;
  578. }
  579. catch
  580. {
  581. return null;
  582. }
  583. }
  584. /// <summary>
  585. /// 创建类型
  586. /// </summary>
  587. /// <param name="modBuilder">模块生成器</param>
  588. /// <param name="layer">图层信息</param>
  589. /// <returns>类型信息</returns>
  590. private static Type CreateTypeToString(ModuleBuilder modBuilder, GISLayer layer, List<string> lstFiledName)
  591. {
  592. TypeBuilder typeBuilder = modBuilder.DefineType(layer.LayerNameEN, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout);
  593.  
  594. CreateConstructor(typeBuilder);
  595. CreatePropertiesToString(typeBuilder, layer.FieldENlist, lstFiledName);
  596.  
  597. return typeBuilder.CreateType();
  598. }
  599.  
  600. /// <summary>
  601. /// 创建类属性信息
  602. /// </summary>
  603. /// <param name="typeBuilder">类型生成器</param>
  604. /// <param name="fieldInfoCollection">图层字段集合</param>
  605. private static void CreatePropertiesToString(TypeBuilder typeBuilder,
  606. IEnumerable<FieldInfo> fieldInfoCollection, List<string> lstFiledName)
  607. {
  608. foreach (FieldInfo fi in fieldInfoCollection)
  609. {
  610. if (fi.IsVisible == true)
  611. {
  612. MethodBuilder setMethodBuilder;
  613. MethodBuilder getMethodBuilder;
  614. FieldBuilder fieldBuilder;
  615. PropertyBuilder propertyBuilder;
  616. MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName |
  617. MethodAttributes.HideBySig;
  618. if (lstFiledName.Contains(fi.FieldENName.ToLower()))
  619. {
  620. fieldBuilder = typeBuilder.DefineField("m" + fi.FieldENName,
  621. typeof(string),
  622. FieldAttributes.Private);
  623. propertyBuilder = typeBuilder.DefineProperty(fi.FieldENName,
  624. PropertyAttributes.HasDefault,
  625. typeof(string),
  626. null);
  627. getMethodBuilder = typeBuilder.DefineMethod("get_" + fi.FieldENName, getSetAttr,
  628. typeof(string),
  629. Type.EmptyTypes);
  630. setMethodBuilder = typeBuilder.DefineMethod("set_" + fi.FieldENName, getSetAttr,
  631. null,
  632. new Type[]
  633. {
  634. typeof(string)
  635. });
  636. }
  637. else
  638. {
  639. fieldBuilder = typeBuilder.DefineField("m" + fi.FieldENName,
  640. ToDotNetTypeFromDBType(fi.FieldType),
  641. FieldAttributes.Private);
  642. propertyBuilder = typeBuilder.DefineProperty(fi.FieldENName,
  643. PropertyAttributes.HasDefault,
  644. ToDotNetTypeFromDBType(fi.FieldType),
  645. null);
  646. getMethodBuilder = typeBuilder.DefineMethod("get_" + fi.FieldENName, getSetAttr,
  647. ToDotNetTypeFromDBType(fi.FieldType),
  648. Type.EmptyTypes);
  649. setMethodBuilder = typeBuilder.DefineMethod("set_" + fi.FieldENName, getSetAttr,
  650. null,
  651. new Type[]
  652. {
  653. ToDotNetTypeFromDBType(
  654. fi.FieldType)
  655. });
  656. }
  657.  
  658. Type[] ctorParams = new Type[] { typeof(string) };
  659. ConstructorInfo classCtorInfo = typeof(DisplayNameAttribute).GetConstructor(ctorParams);
  660. CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo,
  661. new object[] { fi.FieldAlias });
  662. propertyBuilder.SetCustomAttribute(customAttributeBuilder);
  663.  
  664. classCtorInfo = typeof(CategoryAttribute).GetConstructor(ctorParams);
  665. customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, new object[] { fi.Category });
  666. propertyBuilder.SetCustomAttribute(customAttributeBuilder);
  667.  
  668. ctorParams = new Type[] { typeof(int) };
  669. classCtorInfo = typeof(CategoryPriorityAttribute).GetConstructor(ctorParams);
  670. customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo,
  671. new object[]
  672. {
  673. GetCategoryPriorityValueByCategoryName
  674. (fi.Category)
  675. });
  676. propertyBuilder.SetCustomAttribute(customAttributeBuilder);
  677.  
  678. ILGenerator ilGenerator = getMethodBuilder.GetILGenerator();
  679.  
  680. ilGenerator.Emit(OpCodes.Ldarg_0);
  681. ilGenerator.Emit(OpCodes.Ldfld, fieldBuilder);
  682. ilGenerator.Emit(OpCodes.Ret);
  683. ilGenerator = setMethodBuilder.GetILGenerator();
  684. ilGenerator.Emit(OpCodes.Ldarg_0);
  685. ilGenerator.Emit(OpCodes.Ldarg_1);
  686. ilGenerator.Emit(OpCodes.Stfld, fieldBuilder);
  687. ilGenerator.Emit(OpCodes.Ret);
  688.  
  689. propertyBuilder.SetGetMethod(getMethodBuilder);
  690. propertyBuilder.SetSetMethod(setMethodBuilder);
  691. }
  692. }
  693. }
  694.  
  695. //end
  696. #endregion
  697. }
  698. }

Silverlight中如何自己写方法将DataTable转换为PagedCollectionView数据(动态创建类)的更多相关文章

  1. C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法:

    public static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); 将指定数目的字 ...

  2. 【转载】 C#中使用float.Parse方法将字符串转换为Float类型

    在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为单精度Float类型就是一个常见的类型转换操作,float.Parse方法是C#中专门用来将字符串转换为float类型的,f ...

  3. 【转载】C#中使用float.TryParse方法将字符串转换为Float类型

    在C#编程过程中,将字符串string转换为单精度float类型过程中,时常使用float.Parse方法,但float.Parse在无法转换的时候,会抛出程序异常,其实还有个float.TryPar ...

  4. 【转载】C#中使用double.TryParse方法将字符串转换为double类型

    在C#编程过程中,将字符串string转换为double类型过程中,时常使用double.Parse方法,但double.Parse在无法转换的时候,会抛出程序异常,其实还有个double.TryPa ...

  5. 【转载】C#中使用double.Parse方法将字符串转换为双精度double类型

    在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为双精度浮点类型double就是一个常见的类型转换操作,double.Parse方法是C#中专门用来将字符串转换为double ...

  6. 【转载】 C#中使用decimal.Parse方法将字符串转换为十进制decimal类型

    在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为十进制decimal类型就是一个常见的类型转换操作,decimal.Parse方法是C#中专门用来将字符串转换为decima ...

  7. 【转载】C#中使用decimal.TryParse方法将字符串转换为十进制decimal类型

    在C#编程过程中,将字符串string转换为decimal类型过程中,时常使用decimal.Parse方法,但decimal.Parse在无法转换的时候,会抛出程序异常,其实还有个decimal.T ...

  8. 【转载】 C#中使用int.TryParse方法将字符串转换为整型Int类型

    在C#编程过程中,将字符串string转换为整型int过程中,时常使用的转换方法为int.Parse方法,但int.Parse在无法转换的时候,会抛出程序异常,其实还有个int.TryParse方法可 ...

  9. 【转载】C#中使用int.Parse方法将字符串转换为整型Int类型

    在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为Int类型就是一个常见的类型转换操作,int.Parse方法是C#中专门用来将字符串转换为整型int的,int.Parse方 ...

随机推荐

  1. Starting httpd: (98)Address already in use: make_sock: could not bind to address [::]:80

    netstat -tulpn| grep :80 killall -9 httpd /etc/init.d/httpd start  or service httpd start

  2. Online Procurement Auctions for Resource Pooling in Client-Assisted Cloud Storage Systems---INFOCOM 2015

    [标题] [作者] [来源] [对本文评价] [why] 存在的问题 [how] [不足] assumption future work [相关方法或论文] [重点提示] [其它]

  3. java多线程并发编程

    Executor框架 Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService ...

  4. 如何占用你用户的时间 and 如何提高客户的满意度 。 待续

    未来的商业竞争, 可能本质上是在争取客户的时间 嗯..有不定时, 未知的奖励,游戏行业就经常使用, 比如打怪掉装备, 不一定掉什么好东西, 让人充满了期待, 玛雅宝石, 有一定的概率... 觉得公司员 ...

  5. selenium自动化遇见Cannot find class in classpath问题

    今天遇见了Cannot find class in classpath的问题, org.testng.TestNGException: Cannot find class in classpath: ...

  6. js的变量声明以及变量提升

    js的变量声明: js正常的变量声明就不多讲了,形如var a=1;这样的变量声明在实际开发中最常用. var a=1,b=2;这种以逗号分隔开的一次声明多个变量,其实相当于var a=1; var ...

  7. bootstrap如何自定义5等分

    根据bootstrap源码改的1比5的栅格系统 /*5等分媒体查询样式begin*/ .col-xs-1-5,.col-sm-1-5,.col-md-1-5,.col-lg-1-5,.col-xs-4 ...

  8. Oracle中sys和system用户的区别

    1.数据库的启动需要以SYSDBA/SYSOPER身份登录. 2.如果在同一主机上使用IPC连接到数据库使用操作系统授权,登录任何一个用户都可以拥有as sysdba和as sysoper. 3.sy ...

  9. python 元组 字符串 字典 列表嵌套练习题1

    最近学习做的习题,直接复制过来 缩进就乱掉了,所以直接以图片方式上传,题目和答案一起

  10. 添加JUnit到Java Build Path

    1.第一种 新建项目,点击右键,选择properties->Java Build Path->Libraries->add library->JUnit->JUnit4- ...