RelationshipBase.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:RelationshipBase
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/17 9:24:42
  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.Relationship
  14. {
  15. /*
  16. * 关系有个组织类别。一对一,或者一对多
  17. *
  18. * 比如:Connector所属关系一对一,Connector关联关系一对多
  19. *
  20. * 关系分成两类:由属性定义的关系,直接有关系表定义的关系
  21. * a、由属性定义的关系,可以自动解析,属性键值,反射找到对应的属性进行赋值
  22. * b、关系表定义的关系,直接加入document
  23. *
  24. * 通过关系名称进行反射,获取相应的值进行赋值,或者在List集合中加入相关项;
  25. *
  26. * 开放自定义关系:
  27. */
  28. /// <summary>
  29. /// 关系基类
  30. /// </summary>
  31. public class RelationshipBase: BimObject
  32. {
  33. public RelationshipBase()
  34. {
  35. IsProperty = true;
  36. this.ElementType = TypeDefinition.Relationship.ToString();
  37. }
  38. /// <summary>
  39. /// 标志关系是否由属性定义
  40. /// </summary>
  41. public bool IsProperty { get;protected set; }
  42. public void SetPropertyName(string propertyName)
  43. {
  44. PropertyName = propertyName;
  45. }
  46. public void SetElementType(TypeDefinition type)
  47. {
  48. this.ElementType = type.ToString();
  49. PropertyName = type.GetDescription();
  50. }
  51. public string PropertyName { get; private set; }
  52. public virtual void AcceptRelation(BimObject bimObject)
  53. {
  54. if (!IsProperty)
  55. {
  56. return;
  57. }
  58. AcceptRelationInner(bimObject);
  59. }
  60. protected virtual void AcceptRelationInner(BimObject bimObject)
  61. {
  62. }
  63. protected PropertyInfo ParseProperty(BimObject bimObject)
  64. {
  65. var type = bimObject.GetType();
  66. string propertyName = PropertyName;
  67. var key = type.FullName + "_" + propertyName;
  68. var propertyInfo = PropertyCache.GetProperty(key);
  69. if (propertyInfo == null)
  70. {
  71. //缓存解析出来的属性元数据,以便提高解析速度
  72. var property = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
  73. PropertyCache.SetProperty(key,property);
  74. }
  75. return propertyInfo;
  76. }
  77. }
  78. }