using System; using System.Collections.Generic; using System.Reflection; using System.Text; using TEAMModelOS.SDK.Context.Attributes.Azure; using TEAMModelOS.SDK.Context.Exception; namespace TEAMModelOS.SDK.DI.AzureCosmos.Inner { public static class AzureCosmosUtil { public static string GetPartitionKey(Type type) { PropertyInfo[] properties = type.GetProperties(); List attrProperties = new List(); foreach (PropertyInfo property in properties) { if (property.Name.Equals("PartitionKey")) { attrProperties.Add(property); break; } object[] attributes = property.GetCustomAttributes(true); foreach (object attribute in attributes) //2.通过映射,找到成员属性上关联的特性类实例, { if (attribute is PartitionKeyAttribute) { attrProperties.Add(property); } } } if (attrProperties.Count <= 0) { throw new BizException(type.Name + " has no PartitionKey !"); } else { if (attrProperties.Count == 1) { return attrProperties[0].Name; } else { throw new BizException(type.Name + " PartitionKey can only be single!"); } } } } }