2
0

CodeCustomAttribute.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. using System;
  21. using System.IO;
  22. using System.Collections;
  23. using System.Reflection;
  24. using System.Reflection.Emit;
  25. namespace Mono.CodeGeneration
  26. {
  27. public class CodeCustomAttribute
  28. {
  29. public static CodeCustomAttribute Create (Type attributeType)
  30. {
  31. return Create (attributeType, Type.EmptyTypes, new object [0], new string [0], new object [0]);
  32. }
  33. public static CodeCustomAttribute Create (Type attributeType, Type [] ctorArgTypes, object [] ctorArgs, string [] namedArgNames, object [] namedArgValues)
  34. {
  35. MemberInfo [] members = new MemberInfo [namedArgNames.Length];
  36. for (int i = 0; i < namedArgNames.Length; i++) {
  37. members [i] = attributeType.GetField (namedArgNames [i]);
  38. if (members [i] == null)
  39. members [i] = attributeType.GetProperty (namedArgNames [i]);
  40. if (members [i] == null)
  41. throw new ArgumentException (String.Format ("Named argument {0} was not found in attribute type {1}", namedArgNames [i], attributeType));
  42. }
  43. CodeLiteral [] args = new CodeLiteral [ctorArgs.Length];
  44. for (int i = 0; i < args.Length; i++)
  45. args [i] = new CodeLiteral (ctorArgs [i]);
  46. CodeLiteral [] nargs = new CodeLiteral [namedArgValues.Length];
  47. for (int i = 0; i < nargs.Length; i++)
  48. nargs [i] = new CodeLiteral (namedArgValues [i]);
  49. return Create (attributeType, ctorArgTypes, args, members, nargs);
  50. }
  51. public static CodeCustomAttribute Create (Type attributeType, Type [] ctorArgTypes, CodeLiteral [] ctorArgs, MemberInfo [] members, CodeLiteral [] values)
  52. {
  53. ArrayList props = new ArrayList ();
  54. ArrayList pvalues = new ArrayList ();
  55. ArrayList fields = new ArrayList ();
  56. ArrayList fvalues = new ArrayList ();
  57. for (int i = 0; i < members.Length; i++) {
  58. if (members [i] == null)
  59. throw new ArgumentException (String.Format ("MemberInfo at {0} was null for type {1}.", i, attributeType));
  60. if (members [i] is PropertyInfo) {
  61. props.Add ((PropertyInfo) members [i]);
  62. pvalues.Add (values [i].Value);
  63. } else {
  64. fields.Add ((FieldInfo) members [i]);
  65. fvalues.Add (values [i].Value);
  66. }
  67. }
  68. ConstructorInfo ci = attributeType.GetConstructor (ctorArgTypes);
  69. CustomAttributeBuilder cab = new CustomAttributeBuilder (
  70. ci, ctorArgs,
  71. (PropertyInfo []) props.ToArray (typeof (PropertyInfo)), pvalues.ToArray (),
  72. (FieldInfo []) fields.ToArray (typeof (FieldInfo)), fvalues.ToArray ());
  73. CodeCustomAttribute attr = new CodeCustomAttribute (
  74. cab, attributeType, ci, ctorArgs, members, values);
  75. return attr;
  76. }
  77. CustomAttributeBuilder customAttributeBuilder;
  78. Type type;
  79. ConstructorInfo constructor;
  80. CodeLiteral [] ctorArgs;
  81. MemberInfo [] members;
  82. CodeLiteral [] namedArgValues;
  83. public CodeCustomAttribute (
  84. CustomAttributeBuilder attributeBuilder,
  85. Type type,
  86. ConstructorInfo constructor,
  87. CodeLiteral [] ctorArgs,
  88. MemberInfo [] namedArgMembers,
  89. CodeLiteral [] namedArgValues)
  90. {
  91. this.type = type;
  92. this.constructor = constructor;
  93. this.customAttributeBuilder = attributeBuilder;
  94. this.ctorArgs = ctorArgs;
  95. this.members = namedArgMembers;
  96. this.namedArgValues = namedArgValues;
  97. }
  98. public CustomAttributeBuilder Builder {
  99. get { return customAttributeBuilder; }
  100. }
  101. public string PrintCode ()
  102. {
  103. StringWriter sw = new StringWriter ();
  104. CodeWriter cw = new CodeWriter (sw);
  105. PrintCode (cw);
  106. return sw.ToString ();
  107. }
  108. public void PrintCode (CodeWriter cw)
  109. {
  110. cw.Write ("[").Write (type.Name).Write ("(");
  111. if (ctorArgs.Length > 0) {
  112. for (int i = 0; i < ctorArgs.Length - 1; i++) {
  113. ctorArgs [i].PrintCode (cw);
  114. cw.Write (", ");
  115. }
  116. ctorArgs [ctorArgs.Length - 1].PrintCode (cw);
  117. }
  118. if (members.Length > 0) {
  119. if (ctorArgs.Length > 0)
  120. cw.Write (", ");
  121. for (int i = 0; i < members.Length; i++) {
  122. cw.Write (members [i].Name).Write (" = ");
  123. namedArgValues [i].PrintCode (cw);
  124. if (i < members.Length - 1)
  125. cw.Write (", ");
  126. }
  127. }
  128. cw.Write (" )]");
  129. cw.EndLine ();
  130. }
  131. }
  132. }