DataPropertyAttribute.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using Bowin.Common.Utility;
  7. namespace Bowin.Common
  8. {
  9. [AttributeUsage( AttributeTargets.Property, AllowMultiple = false)]
  10. public class DataPropertyAttribute: Attribute
  11. {
  12. private static Dictionary<Type, PropertyInfo[]> dataProperties = new Dictionary<Type, PropertyInfo[]>();
  13. public static PropertyInfo[] GetDataProperties(Type entityType)
  14. {
  15. Guard.ArgumentNotNullOrEmpty(entityType, "entityType");
  16. if (dataProperties.ContainsKey(entityType))
  17. {
  18. return dataProperties[entityType];
  19. }
  20. lock (typeof(DataPropertyAttribute))
  21. {
  22. if (dataProperties.ContainsKey(entityType))
  23. {
  24. return dataProperties[entityType];
  25. }
  26. var properties = (from property in entityType.GetProperties()
  27. where property.GetCustomAttributes(typeof(DataPropertyAttribute), true).Any()
  28. select property).ToArray();
  29. dataProperties[entityType] = properties;
  30. return properties;
  31. }
  32. }
  33. }
  34. }