CodeCustomAttribute.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #if !FULL_AOT_RUNTIME
  21. using System;
  22. using System.IO;
  23. using System.Collections;
  24. using System.Reflection;
  25. using System.Reflection.Emit;
  26. namespace Mono.CodeGeneration
  27. {
  28. public class CodeCustomAttribute
  29. {
  30. public static CodeCustomAttribute Create (Type attributeType)
  31. {
  32. return Create (attributeType, Type.EmptyTypes, new object [0], new string [0], new object [0]);
  33. }
  34. public static CodeCustomAttribute Create (Type attributeType, Type [] ctorArgTypes, object [] ctorArgs, string [] namedArgNames, object [] namedArgValues)
  35. {
  36. MemberInfo [] members = new MemberInfo [namedArgNames.Length];
  37. for (int i = 0; i < namedArgNames.Length; i++) {
  38. members [i] = attributeType.GetField (namedArgNames [i]);
  39. if (members [i] == null)
  40. members [i] = attributeType.GetProperty (namedArgNames [i]);
  41. if (members [i] == null)
  42. throw new ArgumentException (String.Format ("Named argument {0} was not found in attribute type {1}", namedArgNames [i], attributeType));
  43. }
  44. CodeLiteral [] args = new CodeLiteral [ctorArgs.Length];
  45. for (int i = 0; i < args.Length; i++)
  46. args [i] = new CodeLiteral (ctorArgs [i]);
  47. CodeLiteral [] nargs = new CodeLiteral [namedArgValues.Length];
  48. for (int i = 0; i < nargs.Length; i++)
  49. nargs [i] = new CodeLiteral (namedArgValues [i]);
  50. return Create (attributeType, ctorArgTypes, args, members, nargs);
  51. }
  52. public static CodeCustomAttribute Create (Type attributeType, Type [] ctorArgTypes, CodeLiteral [] ctorArgs, MemberInfo [] members, CodeLiteral [] values)
  53. {
  54. ArrayList props = new ArrayList ();
  55. ArrayList pvalues = new ArrayList ();
  56. ArrayList fields = new ArrayList ();
  57. ArrayList fvalues = new ArrayList ();
  58. for (int i = 0; i < members.Length; i++) {
  59. if (members [i] == null)
  60. throw new ArgumentException (String.Format ("MemberInfo at {0} was null for type {1}.", i, attributeType));
  61. if (members [i] is PropertyInfo) {
  62. props.Add ((PropertyInfo) members [i]);
  63. pvalues.Add (values [i].Value);
  64. } else {
  65. fields.Add ((FieldInfo) members [i]);
  66. fvalues.Add (values [i].Value);
  67. }
  68. }
  69. ConstructorInfo ci = attributeType.GetConstructor (ctorArgTypes);
  70. CustomAttributeBuilder cab = new CustomAttributeBuilder (
  71. ci, ctorArgs,
  72. (PropertyInfo []) props.ToArray (typeof (PropertyInfo)), pvalues.ToArray (),
  73. (FieldInfo []) fields.ToArray (typeof (FieldInfo)), fvalues.ToArray ());
  74. CodeCustomAttribute attr = new CodeCustomAttribute (
  75. cab, attributeType, ci, ctorArgs, members, values);
  76. return attr;
  77. }
  78. CustomAttributeBuilder customAttributeBuilder;
  79. Type type;
  80. ConstructorInfo constructor;
  81. CodeLiteral [] ctorArgs;
  82. MemberInfo [] members;
  83. CodeLiteral [] namedArgValues;
  84. public CodeCustomAttribute (
  85. CustomAttributeBuilder attributeBuilder,
  86. Type type,
  87. ConstructorInfo constructor,
  88. CodeLiteral [] ctorArgs,
  89. MemberInfo [] namedArgMembers,
  90. CodeLiteral [] namedArgValues)
  91. {
  92. this.type = type;
  93. this.constructor = constructor;
  94. this.customAttributeBuilder = attributeBuilder;
  95. this.ctorArgs = ctorArgs;
  96. this.members = namedArgMembers;
  97. this.namedArgValues = namedArgValues;
  98. }
  99. public CustomAttributeBuilder Builder {
  100. get { return customAttributeBuilder; }
  101. }
  102. public string PrintCode ()
  103. {
  104. StringWriter sw = new StringWriter ();
  105. CodeWriter cw = new CodeWriter (sw);
  106. PrintCode (cw);
  107. return sw.ToString ();
  108. }
  109. public void PrintCode (CodeWriter cw)
  110. {
  111. cw.Write ("[").Write (type.Name).Write ("(");
  112. if (ctorArgs.Length > 0) {
  113. for (int i = 0; i < ctorArgs.Length - 1; i++) {
  114. ctorArgs [i].PrintCode (cw);
  115. cw.Write (", ");
  116. }
  117. ctorArgs [ctorArgs.Length - 1].PrintCode (cw);
  118. }
  119. if (members.Length > 0) {
  120. if (ctorArgs.Length > 0)
  121. cw.Write (", ");
  122. for (int i = 0; i < members.Length; i++) {
  123. cw.Write (members [i].Name).Write (" = ");
  124. namedArgValues [i].PrintCode (cw);
  125. if (i < members.Length - 1)
  126. cw.Write (", ");
  127. }
  128. }
  129. cw.Write (" )]");
  130. cw.EndLine ();
  131. }
  132. }
  133. }
  134. #endif