2 // System.Reflection.Emit/MethodBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 using System.Reflection;
12 using System.Reflection.Emit;
13 using System.Globalization;
14 using System.Security;
15 using System.Security.Permissions;
16 using System.Runtime.CompilerServices;
17 using System.Runtime.InteropServices;
19 namespace System.Reflection.Emit {
21 public sealed class MethodBuilder : MethodInfo {
22 private RuntimeMethodHandle mhandle;
24 private Type[] parameters;
25 private MethodAttributes attrs;
26 private MethodImplAttributes iattrs;
28 private int table_idx;
30 private ILGenerator ilgen;
31 private TypeBuilder type;
32 private ParameterBuilder[] pinfo;
33 private CustomAttributeBuilder[] cattrs;
34 private MethodInfo override_method;
35 private string pi_dll;
36 private string pi_entry;
37 private CharSet ncharset;
38 private CallingConvention native_cc;
39 private CallingConventions call_conv;
40 private bool init_locals = true;
41 private GenericTypeParameterBuilder[] generic_params;
42 private Type[] returnModReq;
43 private Type[] returnModOpt;
44 private Type[][] paramModReq;
45 private Type[][] paramModOpt;
46 private RefEmitPermissionSet[] permissions;
48 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt) {
50 this.attrs = attributes;
51 this.call_conv = callingConvention;
52 this.rtype = returnType;
53 this.returnModReq = returnModReq;
54 this.returnModOpt = returnModOpt;
55 this.paramModReq = paramModReq;
56 this.paramModOpt = paramModOpt;
57 // The MSDN docs does not specify this, but the MS MethodBuilder
58 // appends a HasThis flag if the method is not static
59 if ((attributes & MethodAttributes.Static) == 0)
60 this.call_conv |= CallingConventions.HasThis;
61 if (parameterTypes != null) {
62 this.parameters = new Type [parameterTypes.Length];
63 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
66 table_idx = get_next_table_index (this, 0x06, true);
67 //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
70 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes,
71 CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt,
72 String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset)
73 : this (tb, name, attributes, callingConvention, returnType, returnModReq, returnModOpt, parameterTypes, paramModReq, paramModOpt) {
76 native_cc = nativeCConv;
77 ncharset = nativeCharset;
80 public bool InitLocals {
81 get {return init_locals;}
82 set {init_locals = value;}
85 internal TypeBuilder TypeBuilder {
89 public override RuntimeMethodHandle MethodHandle {
91 throw NotSupported ();
95 public override Type ReturnType {get {return rtype;}}
96 public override Type ReflectedType {get {return type;}}
97 public override Type DeclaringType {get {return type;}}
98 public override string Name {get {return name;}}
99 public override MethodAttributes Attributes {get {return attrs;}}
100 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
104 public override CallingConventions CallingConvention {
105 get { return call_conv; }
109 public string Signature {
111 throw new NotImplementedException ();
115 public MethodToken GetToken() {
116 return new MethodToken(0x06000000 | table_idx);
119 public override MethodInfo GetBaseDefinition() {
122 public override MethodImplAttributes GetMethodImplementationFlags() {
125 public override ParameterInfo[] GetParameters() {
126 if (parameters == null)
129 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
130 for (int i = 0; i < parameters.Length; i++) {
131 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
136 internal override int GetParameterCount ()
138 if (parameters == null)
141 return parameters.Length;
144 public Module GetModule () {
148 public void CreateMethodBody( byte[] il, int count) {
149 if ((il != null) && ((count < 0) || (count > il.Length)))
150 throw new ArgumentException ("Index was out of range. Must be non-negative and less than the size of the collection.");
152 if ((code != null) || type.is_created)
153 throw new InvalidOperationException ("Type definition of the method is complete.");
158 code = new byte [count];
159 System.Array.Copy(il, code, count);
163 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
164 throw NotSupported ();
166 public override bool IsDefined (Type attribute_type, bool inherit) {
167 throw NotSupported ();
169 public override object[] GetCustomAttributes( bool inherit) {
170 throw NotSupported ();
172 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
173 throw NotSupported ();
175 public ILGenerator GetILGenerator () {
176 return GetILGenerator (64);
179 public ILGenerator GetILGenerator (int size) {
180 if (((iattrs & MethodImplAttributes.CodeTypeMask) !=
181 MethodImplAttributes.IL) ||
182 ((iattrs & MethodImplAttributes.ManagedMask) !=
183 MethodImplAttributes.Managed))
184 throw new InvalidOperationException ("Method body should not exist.");
187 ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), size);
191 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
194 // Extension: Mono allows position == 0 for the return attribute
196 if ((position < 0) || (position > parameters.Length))
197 throw new ArgumentOutOfRangeException ("position");
201 ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
203 pinfo = new ParameterBuilder [parameters.Length + 1];
204 pinfo [position] = pb;
208 internal void fixup () {
210 ilgen.label_fixup ();
213 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
214 if (customBuilder == null)
215 throw new ArgumentNullException ("customBuilder");
216 string attrname = customBuilder.Ctor.ReflectedType.FullName;
217 if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
218 byte[] data = customBuilder.Data;
219 int impla; // the (stupid) ctor takes a short or an int ...
220 impla = (int)data [2];
221 impla |= ((int)data [3]) << 8;
222 SetImplementationFlags ((MethodImplAttributes)impla);
225 if (cattrs != null) {
226 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
227 cattrs.CopyTo (new_array, 0);
228 new_array [cattrs.Length] = customBuilder;
231 cattrs = new CustomAttributeBuilder [1];
232 cattrs [0] = customBuilder;
235 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
237 throw new ArgumentNullException ("con");
238 if (binaryAttribute == null)
239 throw new ArgumentNullException ("binaryAttribute");
240 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
242 public void SetImplementationFlags( MethodImplAttributes attributes) {
247 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
249 throw new ArgumentNullException ("pset");
250 if ((action == SecurityAction.RequestMinimum) ||
251 (action == SecurityAction.RequestOptional) ||
252 (action == SecurityAction.RequestRefuse))
253 throw new ArgumentException ("Request* values are not permitted", "action");
257 if (permissions != null) {
258 /* Check duplicate actions */
259 foreach (RefEmitPermissionSet set in permissions)
260 if (set.action == action)
261 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
263 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
264 permissions.CopyTo (new_array, 0);
265 permissions = new_array;
268 permissions = new RefEmitPermissionSet [1];
270 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
271 attrs |= MethodAttributes.HasSecurity;
275 public void SetMarshal (UnmanagedMarshal unmanagedMarshal)
278 throw new NotImplementedException ();
282 public void SetSymCustomAttribute (string name, byte[] data)
285 throw new NotImplementedException ();
288 internal override int get_next_table_index (object obj, int table, bool inc) {
289 return type.get_next_table_index (obj, table, inc);
292 internal void set_override (MethodInfo mdecl) {
293 override_method = mdecl;
296 private void RejectIfCreated () {
298 throw new InvalidOperationException ("Type definition of the method is complete.");
301 private Exception NotSupported () {
302 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
306 [MethodImplAttribute(MethodImplOptions.InternalCall)]
307 public override extern MethodInfo BindGenericParameters (Type [] types);
309 public override bool Mono_IsInflatedMethod {
315 public override bool HasGenericParameters {
317 return generic_params != null;
321 public override Type[] GetGenericArguments ()
323 if (generic_params == null)
326 Type[] result = new Type [generic_params.Length];
327 for (int i = 0; i < generic_params.Length; i++)
328 result [i] = generic_params [i];
333 public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
335 generic_params = new GenericTypeParameterBuilder [names.Length];
336 for (int i = 0; i < names.Length; i++)
337 generic_params [i] = new GenericTypeParameterBuilder (
338 type, this, names [i], i);
340 return generic_params;
343 public void SetGenericMethodSignature (MethodAttributes attributes, CallingConventions callingConvention, Type return_type, Type[] parameter_types)
347 this.attrs = attributes;
348 this.call_conv = callingConvention;
349 if ((attributes & MethodAttributes.Static) == 0)
350 this.call_conv |= CallingConventions.HasThis;
352 this.rtype = return_type;
353 this.parameters = new Type [parameter_types.Length];
354 System.Array.Copy (parameter_types, this.parameters, parameter_types.Length);