PersonalizableAttribute.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // System.Web.Compilation.PersonalizableAttribute
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Reflection;
  33. namespace System.Web.UI.WebControls.WebParts
  34. {
  35. [AttributeUsageAttribute(AttributeTargets.Property)]
  36. public sealed class PersonalizableAttribute : Attribute
  37. {
  38. public static readonly PersonalizableAttribute Default;
  39. public static readonly PersonalizableAttribute NotPersonalizable;
  40. public static readonly PersonalizableAttribute Personalizable;
  41. public static readonly PersonalizableAttribute SharedPersonalizable;
  42. public static readonly PersonalizableAttribute UserPersonalizable;
  43. bool isPersonalizable;
  44. bool isSensitive;
  45. PersonalizationScope scope;
  46. static PersonalizableAttribute ()
  47. {
  48. Default = new PersonalizableAttribute (false);
  49. NotPersonalizable = Default;
  50. Personalizable = new PersonalizableAttribute (PersonalizationScope.User, false);
  51. SharedPersonalizable = new PersonalizableAttribute (PersonalizationScope.Shared, false);
  52. UserPersonalizable = new PersonalizableAttribute (PersonalizationScope.User, false);
  53. }
  54. public PersonalizableAttribute () : this (true)
  55. {
  56. }
  57. public PersonalizableAttribute (bool isPersonalizable)
  58. {
  59. this.isPersonalizable = isPersonalizable;
  60. this.scope = PersonalizationScope.User;
  61. this.isSensitive = false;
  62. }
  63. public PersonalizableAttribute (PersonalizationScope scope) : this (scope, false)
  64. {
  65. }
  66. public PersonalizableAttribute (PersonalizationScope scope, bool isSensitive)
  67. {
  68. this.isPersonalizable = true;
  69. this.scope = scope;
  70. this.isSensitive = isSensitive;
  71. }
  72. public bool IsPersonalizable {
  73. get { return isPersonalizable; }
  74. }
  75. public bool IsSensitive {
  76. get { return isSensitive; }
  77. }
  78. public PersonalizationScope Scope {
  79. get { return scope; }
  80. }
  81. public override bool Equals (object obj)
  82. {
  83. PersonalizableAttribute attr = obj as PersonalizableAttribute;
  84. if (attr == null)
  85. return false;
  86. return (this.isPersonalizable == attr.IsPersonalizable &&
  87. this.isSensitive == attr.IsSensitive &&
  88. this.scope == attr.Scope);
  89. }
  90. public override int GetHashCode ()
  91. {
  92. return (this.isPersonalizable.GetHashCode () ^
  93. this.isSensitive.GetHashCode () ^
  94. this.scope.GetHashCode ());
  95. }
  96. public static ICollection GetPersonalizableProperties (Type type)
  97. {
  98. if (type == null)
  99. throw new ArgumentNullException ("type");
  100. PropertyInfo[] properties = type.GetProperties ();
  101. if (properties == null || properties.Length == 0)
  102. return new PropertyInfo [0];
  103. List <PropertyInfo> ret = null;
  104. foreach (PropertyInfo pi in properties)
  105. if (PropertyQualifies (pi)) {
  106. if (ret == null)
  107. ret = new List <PropertyInfo> ();
  108. ret.Add (pi);
  109. }
  110. return ret;
  111. }
  112. static bool PropertyQualifies (PropertyInfo pi)
  113. {
  114. object[] attributes = pi.GetCustomAttributes (false);
  115. if (attributes == null || attributes.Length == 0)
  116. return false;
  117. PersonalizableAttribute attr;
  118. MethodInfo mi;
  119. foreach (object a in attributes) {
  120. attr = a as PersonalizableAttribute;
  121. if (attr == null || !attr.IsPersonalizable)
  122. continue;
  123. mi = pi.GetSetMethod (false);
  124. if (mi == null)
  125. throw new HttpException ("A public property on the type is marked as personalizable but is read-only.");
  126. return true;
  127. }
  128. return false;
  129. }
  130. public override bool IsDefaultAttribute ()
  131. {
  132. return PersonalizableAttribute.Equals (this, Default);
  133. }
  134. public override bool Match (object obj)
  135. {
  136. PersonalizableAttribute attr = obj as PersonalizableAttribute;
  137. if (obj == null)
  138. return false;
  139. return (this.isPersonalizable == attr.IsPersonalizable);
  140. }
  141. }
  142. }