CodeProperty.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. //
  21. // Copyright (C) Lluis Sanchez Gual, 2004
  22. //
  23. using System;
  24. using System.Collections;
  25. using System.IO;
  26. using System.Reflection;
  27. using System.Reflection.Emit;
  28. namespace Mono.CodeGeneration
  29. {
  30. public class CodeProperty
  31. {
  32. PropertyInfo propertyInfo;
  33. CodeBuilder get_builder;
  34. CodeBuilder set_builder;
  35. string name;
  36. PropertyAttributes attributes;
  37. MethodAttributes methodAttributes;
  38. Type returnType;
  39. TypeBuilder typeBuilder;
  40. Type[] parameterTypes;
  41. ArrayList customAttributes = new ArrayList ();
  42. CodeClass cls;
  43. internal static CodeProperty DefineProperty (CodeClass cls, string name, PropertyAttributes attributes, MethodAttributes methodAttributes, Type returnType, Type[] parameterTypes)
  44. {
  45. return new CodeProperty (cls, name, attributes, methodAttributes, returnType, parameterTypes);
  46. }
  47. internal CodeProperty (CodeClass cls, string name, PropertyAttributes attributes, MethodAttributes methodAttributes, Type returnType, Type[] parameterTypes)
  48. {
  49. this.cls = cls;
  50. this.typeBuilder = cls.TypeBuilder;
  51. this.name = name;
  52. this.attributes = attributes;
  53. this.methodAttributes = methodAttributes;
  54. this.returnType = returnType;
  55. this.parameterTypes = parameterTypes;
  56. PropertyBuilder pb = typeBuilder.DefineProperty (name, attributes, returnType, parameterTypes);
  57. pb.SetGetMethod (typeBuilder.DefineMethod ("get_" + name, methodAttributes, CallingConventions.Standard, returnType, Type.EmptyTypes));
  58. pb.SetSetMethod (typeBuilder.DefineMethod ("set_" + name, methodAttributes, CallingConventions.Standard, typeof (void), new Type [] {returnType}));
  59. get_builder = new CodeBuilder (cls);
  60. set_builder = new CodeBuilder (cls);
  61. propertyInfo = pb;
  62. }
  63. public TypeBuilder DeclaringType
  64. {
  65. get { return typeBuilder; }
  66. }
  67. public PropertyBuilder PropertyBuilder
  68. {
  69. get { return propertyInfo as PropertyBuilder; }
  70. }
  71. public string Name
  72. {
  73. get { return name; }
  74. }
  75. public PropertyAttributes Attributes
  76. {
  77. get { return attributes; }
  78. }
  79. public Type ReturnType
  80. {
  81. get { return returnType; }
  82. }
  83. public Type[] ParameterTypes
  84. {
  85. get { return parameterTypes; }
  86. }
  87. public CodeBuilder CodeBuilderGet
  88. {
  89. get { return get_builder; }
  90. }
  91. public CodeBuilder CodeBuilderSet
  92. {
  93. get { return set_builder; }
  94. }
  95. public bool IsStatic
  96. {
  97. get { return (methodAttributes & MethodAttributes.Static) != 0; }
  98. }
  99. public bool IsPublic
  100. {
  101. get { return (methodAttributes & MethodAttributes.Public) != 0; }
  102. }
  103. public CodeCustomAttribute CreateCustomAttribute (Type attributeType)
  104. {
  105. return CreateCustomAttribute (attributeType,
  106. Type.EmptyTypes, new object [0]);
  107. }
  108. public CodeCustomAttribute CreateCustomAttribute (Type attributeType, Type [] ctorArgTypes, object [] ctorArgs)
  109. {
  110. return CreateCustomAttribute (attributeType,
  111. ctorArgTypes, ctorArgs, new string [0], new object [0]);
  112. }
  113. public CodeCustomAttribute CreateCustomAttribute (Type attributeType, Type [] ctorArgTypes, object [] ctorArgs, string [] namedArgFieldNames, object [] namedArgValues)
  114. {
  115. CodeCustomAttribute cca = CodeCustomAttribute.Create (
  116. attributeType, ctorArgTypes, ctorArgs, namedArgFieldNames, namedArgValues);
  117. SetCustomAttribute (cca);
  118. return cca;
  119. }
  120. public CodeCustomAttribute CreateCustomAttribute (Type attributeType, Type [] ctorArgTypes, CodeLiteral [] ctorArgs, FieldInfo [] fields, CodeLiteral [] fieldValues)
  121. {
  122. CodeCustomAttribute cca = CodeCustomAttribute.Create (
  123. attributeType, ctorArgTypes, ctorArgs, fields, fieldValues);
  124. SetCustomAttribute (cca);
  125. return cca;
  126. }
  127. void SetCustomAttribute (CodeCustomAttribute cca)
  128. {
  129. PropertyBuilder.SetCustomAttribute (cca.Builder);
  130. customAttributes.Add (cca);
  131. }
  132. public string PrintCode ()
  133. {
  134. StringWriter sw = new StringWriter ();
  135. CodeWriter cw = new CodeWriter (sw);
  136. PrintCode (cw);
  137. return sw.ToString ();
  138. }
  139. public void PrintCode (CodeWriter cp)
  140. {
  141. cp.BeginLine ();
  142. foreach (CodeCustomAttribute a in customAttributes)
  143. a.PrintCode (cp);
  144. cp.BeginLine ();
  145. if (IsStatic)
  146. cp.Write ("static ");
  147. if (IsPublic)
  148. cp.Write ("public ");
  149. if (returnType != null) cp.Write (returnType + " ");
  150. cp.Write (name);
  151. if (parameterTypes.Length > 0) {
  152. cp.Write (name + " [");
  153. for (int n=0; n<parameterTypes.Length; n++) {
  154. if (n > 0) cp.Write (", ");
  155. cp.Write (parameterTypes[n] + " arg" + n);
  156. }
  157. cp.Write ("]");
  158. }
  159. cp.Write (" {");
  160. cp.EndLine ();
  161. cp.Indent ();
  162. cp.WriteLineInd ("get {");
  163. get_builder.PrintCode (cp);
  164. cp.WriteLineUnind ("}");
  165. cp.WriteLine ("set {");
  166. set_builder.PrintCode (cp);
  167. cp.WriteLine ("}");
  168. cp.WriteLineUnind ("}");
  169. }
  170. public CodeArgumentReference GetArg (int n)
  171. {
  172. if (n < 0 || n >= parameterTypes.Length)
  173. throw new InvalidOperationException ("Invalid argument number");
  174. int narg = IsStatic ? n : n + 1;
  175. return new CodeArgumentReference (parameterTypes[n], narg, "arg" + n);
  176. }
  177. public CodeArgumentReference GetThis ()
  178. {
  179. if (IsStatic)
  180. throw new InvalidOperationException ("'this' not available in static methods");
  181. return new CodeArgumentReference (DeclaringType, 0, "this");
  182. }
  183. public void Generate ()
  184. {
  185. ILGenerator gen;
  186. Label returnLabel;
  187. MethodBuilder mb;
  188. // getter
  189. mb = (MethodBuilder) propertyInfo.GetGetMethod ();
  190. if (mb != null) {
  191. gen = mb.GetILGenerator();
  192. returnLabel = gen.DefineLabel ();
  193. get_builder.ReturnLabel = returnLabel;
  194. get_builder.Generate (gen);
  195. gen.MarkLabel (returnLabel);
  196. gen.Emit (OpCodes.Ret);
  197. }
  198. // setter
  199. mb = (MethodBuilder) propertyInfo.GetSetMethod ();
  200. if (mb != null) {
  201. gen = mb.GetILGenerator();
  202. returnLabel = gen.DefineLabel ();
  203. set_builder.ReturnLabel = returnLabel;
  204. set_builder.Generate (gen);
  205. gen.MarkLabel (returnLabel);
  206. gen.Emit (OpCodes.Ret);
  207. }
  208. }
  209. public void UpdatePropertyInfo (Type type)
  210. {
  211. propertyInfo = type.GetProperty (propertyInfo.Name, parameterTypes);
  212. }
  213. }
  214. }