BimObjectUtil.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:BimObjectUtil
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/18 15:13:11
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace JBIM.Common
  14. {
  15. public static class BimObjectUtil
  16. {
  17. public static void AcceptRelation<T>(BimObject bimObject, string property,T value)
  18. {
  19. var propertyInfo=ParseProperty(bimObject, property);
  20. if (propertyInfo != null)
  21. {
  22. if (propertyInfo.PropertyType.IsInstanceOfType(value))
  23. {
  24. propertyInfo.SetValue(bimObject, value);
  25. }
  26. }
  27. }
  28. /// <summary>
  29. /// 增加单关联信息
  30. /// </summary>
  31. /// <param name="bimObject"></param>
  32. /// <param name="property"></param>
  33. /// <param name="value"></param>
  34. public static void AcceptRelation(BimObject bimObject, string property, BimId value)
  35. {
  36. AcceptRelation<BimId>(bimObject, property, value);
  37. }
  38. /// <summary>
  39. /// 增加一对多关联信息
  40. /// </summary>
  41. /// <param name="bimObject"></param>
  42. /// <param name="property"></param>
  43. /// <param name="value"></param>
  44. public static void AcceptRelations(BimObject bimObject, string property,List<BimId> value)
  45. {
  46. AcceptRelation<List<BimId>>(bimObject, property, value);
  47. }
  48. private static PropertyInfo ParseProperty(BimObject bimObject,string pName)
  49. {
  50. var type = bimObject.GetType();
  51. string propertyName = pName;
  52. var key = type.FullName + "_" + propertyName;
  53. var propertyInfo = PropertyCache.GetProperty(key);
  54. if (propertyInfo == null)
  55. {
  56. //缓存解析出来的属性元数据,以便提高解析速度
  57. propertyInfo = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
  58. if(propertyInfo!=null)
  59. {
  60. PropertyCache.SetProperty(key, propertyInfo);
  61. }
  62. }
  63. return propertyInfo;
  64. }
  65. }
  66. }