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.Security;
16 using System.Security.Permissions;
17 using System.Runtime.CompilerServices;
18 using System.Runtime.InteropServices;
20 namespace System.Reflection.Emit {
22 public sealed class MethodBuilder : MethodInfo {
23 private RuntimeMethodHandle mhandle;
25 private Type[] parameters;
26 private MethodAttributes attrs;
27 private MethodImplAttributes iattrs;
29 private int table_idx;
31 private ILGenerator ilgen;
32 private TypeBuilder type;
33 private ParameterBuilder[] pinfo;
34 private CustomAttributeBuilder[] cattrs;
35 private MethodInfo override_method;
36 private string pi_dll;
37 private string pi_entry;
38 private CharSet ncharset;
39 private CallingConvention native_cc;
40 private CallingConventions call_conv;
41 private bool init_locals = true;
42 private MonoGenericParam[] generic_params;
43 private Type[] returnModReq;
44 private Type[] returnModOpt;
45 private Type[][] paramModReq;
46 private Type[][] paramModOpt;
47 private RefEmitPermissionSet[] permissions;
49 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt) {
51 this.attrs = attributes;
52 this.call_conv = callingConvention;
53 this.rtype = returnType;
54 this.returnModReq = returnModReq;
55 this.returnModOpt = returnModOpt;
56 this.paramModReq = paramModReq;
57 this.paramModOpt = paramModOpt;
58 // The MSDN docs does not specify this, but the MS MethodBuilder
59 // appends a HasThis flag if the method is not static
60 if ((attributes & MethodAttributes.Static) == 0)
61 this.call_conv |= CallingConventions.HasThis;
62 if (parameterTypes != null) {
63 this.parameters = new Type [parameterTypes.Length];
64 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
67 table_idx = get_next_table_index (this, 0x06, true);
68 //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
71 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes,
72 CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt,
73 String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset)
74 : this (tb, name, attributes, callingConvention, returnType, returnModReq, returnModOpt, parameterTypes, paramModReq, paramModOpt) {
77 native_cc = nativeCConv;
78 ncharset = nativeCharset;
81 public bool InitLocals {
82 get {return init_locals;}
83 set {init_locals = value;}
86 internal TypeBuilder TypeBuilder {
90 public override RuntimeMethodHandle MethodHandle {
92 throw NotSupported ();
96 public override Type ReturnType {get {return rtype;}}
97 public override Type ReflectedType {get {return type;}}
98 public override Type DeclaringType {get {return type;}}
99 public override string Name {get {return name;}}
100 public override MethodAttributes Attributes {get {return attrs;}}
101 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
105 public override CallingConventions CallingConvention {
106 get { return call_conv; }
110 public string Signature {
112 throw new NotImplementedException ();
116 public MethodToken GetToken() {
117 return new MethodToken(0x06000000 | table_idx);
120 public override MethodInfo GetBaseDefinition() {
123 public override MethodImplAttributes GetMethodImplementationFlags() {
126 public override ParameterInfo[] GetParameters() {
127 if (parameters == null)
130 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
131 for (int i = 0; i < parameters.Length; i++) {
132 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
137 internal override int GetParameterCount ()
139 if (parameters == null)
142 return parameters.Length;
145 public Module GetModule () {
149 public void CreateMethodBody( byte[] il, int count) {
150 if ((il != null) && ((count < 0) || (count > il.Length)))
151 throw new ArgumentException ("Index was out of range. Must be non-negative and less than the size of the collection.");
153 if ((code != null) || type.is_created)
154 throw new InvalidOperationException ("Type definition of the method is complete.");
159 code = new byte [count];
160 System.Array.Copy(il, code, count);
164 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
165 throw NotSupported ();
167 public override bool IsDefined (Type attribute_type, bool inherit) {
168 throw NotSupported ();
170 public override object[] GetCustomAttributes( bool inherit) {
171 throw NotSupported ();
173 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
174 throw NotSupported ();
176 public ILGenerator GetILGenerator () {
177 return GetILGenerator (64);
180 public ILGenerator GetILGenerator (int size) {
181 if (((iattrs & MethodImplAttributes.CodeTypeMask) !=
182 MethodImplAttributes.IL) ||
183 ((iattrs & MethodImplAttributes.ManagedMask) !=
184 MethodImplAttributes.Managed))
185 throw new InvalidOperationException ("Method body should not exist.");
188 ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), size);
192 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
195 // Extension: Mono allows position == 0 for the return attribute
197 if ((position < 0) || (position > parameters.Length))
198 throw new ArgumentOutOfRangeException ("position");
202 ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
204 pinfo = new ParameterBuilder [parameters.Length + 1];
205 pinfo [position] = pb;
209 internal void fixup () {
211 ilgen.label_fixup ();
214 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
215 if (customBuilder == null)
216 throw new ArgumentNullException ("customBuilder");
217 string attrname = customBuilder.Ctor.ReflectedType.FullName;
218 if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
219 byte[] data = customBuilder.Data;
220 int impla; // the (stupid) ctor takes a short or an int ...
221 impla = (int)data [2];
222 impla |= ((int)data [3]) << 8;
223 SetImplementationFlags ((MethodImplAttributes)impla);
226 if (cattrs != null) {
227 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
228 cattrs.CopyTo (new_array, 0);
229 new_array [cattrs.Length] = customBuilder;
232 cattrs = new CustomAttributeBuilder [1];
233 cattrs [0] = customBuilder;
236 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
238 throw new ArgumentNullException ("con");
239 if (binaryAttribute == null)
240 throw new ArgumentNullException ("binaryAttribute");
241 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
243 public void SetImplementationFlags( MethodImplAttributes attributes) {
248 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
250 throw new ArgumentNullException ("pset");
251 if ((action == SecurityAction.RequestMinimum) ||
252 (action == SecurityAction.RequestOptional) ||
253 (action == SecurityAction.RequestRefuse))
254 throw new ArgumentException ("Request* values are not permitted", "action");
258 if (permissions != null) {
259 /* Check duplicate actions */
260 foreach (RefEmitPermissionSet set in permissions)
261 if (set.action == action)
262 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
264 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
265 permissions.CopyTo (new_array, 0);
266 permissions = new_array;
269 permissions = new RefEmitPermissionSet [1];
271 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
272 attrs |= MethodAttributes.HasSecurity;
276 public void SetMarshal (UnmanagedMarshal unmanagedMarshal)
279 throw new NotImplementedException ();
283 public void SetSymCustomAttribute (string name, byte[] data)
286 throw new NotImplementedException ();
289 internal override int get_next_table_index (object obj, int table, bool inc) {
290 return type.get_next_table_index (obj, table, inc);
293 internal void set_override (MethodInfo mdecl) {
294 override_method = mdecl;
297 private void RejectIfCreated () {
299 throw new InvalidOperationException ("Type definition of the method is complete.");
302 private Exception NotSupported () {
303 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
307 [MethodImplAttribute(MethodImplOptions.InternalCall)]
308 public override extern MethodInfo BindGenericParameters (Type [] types);
310 [MethodImplAttribute(MethodImplOptions.InternalCall)]
311 private extern MonoGenericParam define_generic_parameter (string name, int index);
313 public Type DefineGenericParameter (string name)
316 if (generic_params != null) {
317 MonoGenericParam[] new_generic_params = new MonoGenericParam [generic_params.Length+1];
318 System.Array.Copy (generic_params, new_generic_params, generic_params.Length);
319 index = generic_params.Length;
320 generic_params = new_generic_params;
322 generic_params = new MonoGenericParam [1];
326 generic_params [index] = define_generic_parameter (name, index);
327 return generic_params [index];
330 public void SetGenericParameterConstraints (int index, Type[] constraints, bool has_ctor_constraint)
332 generic_params [index].SetConstraints (constraints, has_ctor_constraint);
335 public override bool Mono_IsInflatedMethod {
341 public override bool HasGenericParameters {
343 return generic_params != null;
347 public override Type[] GetGenericArguments ()
349 if (generic_params == null)
352 Type[] result = new Type [generic_params.Length];
353 for (int i = 0; i < generic_params.Length; i++)
354 result [i] = generic_params [i];
359 public void SetGenericMethodSignature (MethodAttributes attributes, CallingConventions callingConvention, Type return_type, Type[] parameter_types)
363 this.attrs = attributes;
364 this.call_conv = callingConvention;
365 if ((attributes & MethodAttributes.Static) == 0)
366 this.call_conv |= CallingConventions.HasThis;
368 this.rtype = return_type;
369 this.parameters = new Type [parameter_types.Length];
370 System.Array.Copy (parameter_types, this.parameters, parameter_types.Length);