3 // System.Reflection.Emit/MethodBuilder.cs
6 // Paolo Molaro (lupus@ximian.com)
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Globalization;
15 using System.Runtime.CompilerServices;
16 using System.Runtime.InteropServices;
18 namespace System.Reflection.Emit {
20 public sealed class MethodBuilder : MethodInfo {
21 private RuntimeMethodHandle mhandle;
23 private Type[] parameters;
24 private MethodAttributes attrs;
25 private MethodImplAttributes iattrs;
27 private int table_idx;
29 private ILGenerator ilgen;
30 private TypeBuilder type;
31 private ParameterBuilder[] pinfo;
32 private CustomAttributeBuilder[] cattrs;
33 private MethodInfo override_method;
34 private string pi_dll;
35 private string pi_entry;
36 private CharSet ncharset;
37 private CallingConvention native_cc;
38 private CallingConventions call_conv;
39 private bool init_locals = true;
40 private TypeBuilder.MonoGenericParam[] generic_params;
41 private Type[] returnModReq;
42 private Type[] returnModOpt;
43 private Type[][] paramModReq;
44 private Type[][] paramModOpt;
46 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt) {
48 this.attrs = attributes;
49 this.call_conv = callingConvention;
50 this.rtype = returnType;
51 this.returnModReq = returnModReq;
52 this.returnModOpt = returnModOpt;
53 this.paramModReq = paramModReq;
54 this.paramModOpt = paramModOpt;
55 // The MSDN docs does not specify this, but the MS MethodBuilder
56 // appends a HasThis flag if the method is not static
57 if ((attributes & MethodAttributes.Static) == 0)
58 this.call_conv |= CallingConventions.HasThis;
59 if (parameterTypes != null) {
60 this.parameters = new Type [parameterTypes.Length];
61 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
64 table_idx = get_next_table_index (this, 0x06, true);
65 //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
68 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes,
69 CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt,
70 String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset)
71 : this (tb, name, attributes, callingConvention, returnType, returnModReq, returnModOpt, parameterTypes, paramModReq, paramModOpt) {
74 native_cc = nativeCConv;
75 ncharset = nativeCharset;
78 public bool InitLocals {
79 get {return init_locals;}
80 set {init_locals = value;}
83 internal TypeBuilder TypeBuilder {
87 public override RuntimeMethodHandle MethodHandle {
89 throw NotSupported ();
93 public override Type ReturnType {get {return rtype;}}
94 public override Type ReflectedType {get {return type;}}
95 public override Type DeclaringType {get {return type;}}
96 public override string Name {get {return name;}}
97 public override MethodAttributes Attributes {get {return attrs;}}
98 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
102 public override CallingConventions CallingConvention {
103 get { return call_conv; }
106 public MethodToken GetToken() {
107 return new MethodToken(0x06000000 | table_idx);
110 public override MethodInfo GetBaseDefinition() {
113 public override MethodImplAttributes GetMethodImplementationFlags() {
116 public override ParameterInfo[] GetParameters() {
117 if (parameters == null)
120 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
121 for (int i = 0; i < parameters.Length; i++) {
122 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
127 public Module GetModule () {
131 public void CreateMethodBody( byte[] il, int count) {
132 if ((il != null) && ((count < 0) || (count > il.Length)))
133 throw new ArgumentException ("Index was out of range. Must be non-negative and less than the size of the collection.");
135 if ((code != null) || type.is_created)
136 throw new InvalidOperationException ("Type definition of the method is complete.");
141 code = new byte [count];
142 System.Array.Copy(il, code, count);
146 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
147 throw NotSupported ();
149 public override bool IsDefined (Type attribute_type, bool inherit) {
150 throw NotSupported ();
152 public override object[] GetCustomAttributes( bool inherit) {
153 throw NotSupported ();
155 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
156 throw NotSupported ();
158 public ILGenerator GetILGenerator () {
159 return GetILGenerator (64);
162 public ILGenerator GetILGenerator (int size) {
163 if (((iattrs & MethodImplAttributes.CodeTypeMask) !=
164 MethodImplAttributes.IL) ||
165 ((iattrs & MethodImplAttributes.ManagedMask) !=
166 MethodImplAttributes.Managed))
167 throw new InvalidOperationException ("Method body should not exist.");
170 ilgen = new ILGenerator (this, size);
174 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
177 // Extension: Mono allows position == 0 for the return attribute
179 if ((position < 0) || (position > parameters.Length))
180 throw new ArgumentOutOfRangeException ("position");
184 ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
186 pinfo = new ParameterBuilder [parameters.Length + 1];
187 pinfo [position] = pb;
191 internal void fixup () {
193 ilgen.label_fixup ();
196 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
197 if (customBuilder == null)
198 throw new ArgumentNullException ("customBuilder");
199 string attrname = customBuilder.Ctor.ReflectedType.FullName;
200 if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
201 byte[] data = customBuilder.Data;
202 int impla; // the (stupid) ctor takes a short or an int ...
203 impla = (int)data [2];
204 impla |= ((int)data [3]) << 8;
205 SetImplementationFlags ((MethodImplAttributes)impla);
208 if (cattrs != null) {
209 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
210 cattrs.CopyTo (new_array, 0);
211 new_array [cattrs.Length] = customBuilder;
214 cattrs = new CustomAttributeBuilder [1];
215 cattrs [0] = customBuilder;
218 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
220 throw new ArgumentNullException ("con");
221 if (binaryAttribute == null)
222 throw new ArgumentNullException ("binaryAttribute");
223 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
225 public void SetImplementationFlags( MethodImplAttributes attributes) {
229 internal override int get_next_table_index (object obj, int table, bool inc) {
230 return type.get_next_table_index (obj, table, inc);
233 internal void set_override (MethodInfo mdecl) {
234 override_method = mdecl;
237 private void RejectIfCreated () {
239 throw new InvalidOperationException ("Type definition of the method is complete.");
242 private Exception NotSupported () {
243 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
247 [MethodImplAttribute(MethodImplOptions.InternalCall)]
248 private extern Type define_generic_parameter (TypeBuilder.MonoGenericParam param);
250 public Type DefineGenericParameter (string name, Type[] constraints)
252 TypeBuilder.MonoGenericParam gparam = new TypeBuilder.MonoGenericParam (name, constraints);
254 if (generic_params != null) {
255 TypeBuilder.MonoGenericParam[] new_generic_params = new TypeBuilder.MonoGenericParam [generic_params.Length+1];
256 System.Array.Copy (generic_params, new_generic_params, generic_params.Length);
257 new_generic_params [generic_params.Length] = gparam;
258 generic_params = new_generic_params;
260 generic_params = new TypeBuilder.MonoGenericParam [1];
261 generic_params [0] = gparam;
264 return define_generic_parameter (gparam);
267 public override Type[] GetGenericArguments ()
269 if (generic_params == null)
272 Type[] result = new Type [generic_params.Length];
273 for (int i = 0; i < generic_params.Length; i++)
274 result [i] = generic_params [i].Type;
279 public void SetGenericMethodSignature (Type return_type, Type[] parameter_types)
283 this.rtype = return_type;
284 this.parameters = new Type [parameter_types.Length];
285 System.Array.Copy (parameter_types, this.parameters, parameter_types.Length);