AzureCosmosUtil.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text;
  5. using TEAMModelOS.SDK.Context.Attributes.Azure;
  6. using TEAMModelOS.SDK.Context.Exception;
  7. namespace TEAMModelOS.SDK.DI.AzureCosmos.Inner
  8. {
  9. public static class AzureCosmosUtil
  10. {
  11. public static string GetPartitionKey(Type type)
  12. {
  13. PropertyInfo[] properties = type.GetProperties();
  14. List<PropertyInfo> attrProperties = new List<PropertyInfo>();
  15. foreach (PropertyInfo property in properties)
  16. {
  17. if (property.Name.Equals("PartitionKey"))
  18. {
  19. attrProperties.Add(property);
  20. break;
  21. }
  22. object[] attributes = property.GetCustomAttributes(true);
  23. foreach (object attribute in attributes) //2.通过映射,找到成员属性上关联的特性类实例,
  24. {
  25. if (attribute is PartitionKeyAttribute)
  26. {
  27. attrProperties.Add(property);
  28. }
  29. }
  30. }
  31. if (attrProperties.Count <= 0)
  32. {
  33. throw new BizException(type.Name + " has no PartitionKey !");
  34. }
  35. else
  36. {
  37. if (attrProperties.Count == 1)
  38. {
  39. return attrProperties[0].Name;
  40. }
  41. else { throw new BizException(type.Name + " PartitionKey can only be single!"); }
  42. }
  43. }
  44. }
  45. }