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;
41 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
43 this.attrs = attributes;
44 this.call_conv = callingConvention;
45 this.rtype = returnType;
46 // The MSDN docs does not specify this, but the MS MethodBuilder
47 // appends a HasThis flag if the method is not static
48 if ((attributes & MethodAttributes.Static) == 0)
49 this.call_conv |= CallingConventions.HasThis;
50 if (parameterTypes != null) {
51 this.parameters = new Type [parameterTypes.Length];
52 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
55 table_idx = get_next_table_index (this, 0x06, true);
56 //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
59 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes,
60 CallingConventions callingConvention, Type returnType, Type[] parameterTypes,
61 String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset)
62 : this (tb, name, attributes, callingConvention, returnType, parameterTypes) {
65 native_cc = nativeCConv;
66 ncharset = nativeCharset;
69 public bool InitLocals {
70 get {return init_locals;}
71 set {init_locals = value;}
74 internal TypeBuilder TypeBuilder {
78 public override RuntimeMethodHandle MethodHandle {
80 throw NotSupported ();
84 public override Type ReturnType {get {return rtype;}}
85 public override Type ReflectedType {get {return type;}}
86 public override Type DeclaringType {get {return type;}}
87 public override string Name {get {return name;}}
88 public override MethodAttributes Attributes {get {return attrs;}}
89 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
93 public override CallingConventions CallingConvention {
94 get { return call_conv; }
97 public MethodToken GetToken() {
98 return new MethodToken(0x06000000 | table_idx);
101 public override MethodInfo GetBaseDefinition() {
104 public override MethodImplAttributes GetMethodImplementationFlags() {
107 public override ParameterInfo[] GetParameters() {
108 if (parameters == null)
111 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
112 for (int i = 0; i < parameters.Length; i++) {
113 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
118 public Module GetModule () {
122 public void CreateMethodBody( byte[] il, int count) {
123 if ((il != null) && ((count < 0) || (count > il.Length)))
124 throw new ArgumentException ("Index was out of range. Must be non-negative and less than the size of the collection.");
126 if ((code != null) || type.is_created)
127 throw new InvalidOperationException ("Type definition of the method is complete.");
132 code = new byte [count];
133 System.Array.Copy(il, code, count);
137 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
138 throw NotSupported ();
140 public override bool IsDefined (Type attribute_type, bool inherit) {
141 throw NotSupported ();
143 public override object[] GetCustomAttributes( bool inherit) {
144 throw NotSupported ();
146 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
147 throw NotSupported ();
149 public ILGenerator GetILGenerator () {
150 return GetILGenerator (64);
153 public ILGenerator GetILGenerator (int size) {
154 if (((iattrs & MethodImplAttributes.CodeTypeMask) !=
155 MethodImplAttributes.IL) ||
156 ((iattrs & MethodImplAttributes.ManagedMask) !=
157 MethodImplAttributes.Managed))
158 throw new InvalidOperationException ("Method body should not exist.");
161 ilgen = new ILGenerator (this, size);
165 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
168 // Extension: Mono allows position == 0 for the return attribute
170 if ((position < 0) || (position > parameters.Length))
171 throw new ArgumentOutOfRangeException ("position");
175 ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
177 pinfo = new ParameterBuilder [parameters.Length + 1];
178 pinfo [position] = pb;
182 internal void fixup () {
184 ilgen.label_fixup ();
187 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
188 if (customBuilder == null)
189 throw new ArgumentNullException ("customBuilder");
190 string attrname = customBuilder.Ctor.ReflectedType.FullName;
191 if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
192 byte[] data = customBuilder.Data;
193 int impla; // the (stupid) ctor takes a short or an int ...
194 impla = (int)data [2];
195 impla |= ((int)data [3]) << 8;
196 SetImplementationFlags ((MethodImplAttributes)impla);
199 if (cattrs != null) {
200 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
201 cattrs.CopyTo (new_array, 0);
202 new_array [cattrs.Length] = customBuilder;
205 cattrs = new CustomAttributeBuilder [1];
206 cattrs [0] = customBuilder;
209 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
211 throw new ArgumentNullException ("con");
212 if (binaryAttribute == null)
213 throw new ArgumentNullException ("binaryAttribute");
214 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
216 public void SetImplementationFlags( MethodImplAttributes attributes) {
220 internal override int get_next_table_index (object obj, int table, bool inc) {
221 return type.get_next_table_index (obj, table, inc);
224 internal void set_override (MethodInfo mdecl) {
225 override_method = mdecl;
228 private void RejectIfCreated () {
230 throw new InvalidOperationException ("Type definition of the method is complete.");
233 private Exception NotSupported () {
234 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");