We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Reflection.Emit
- {
- using System.Text;
- using System;
- using CultureInfo = System.Globalization.CultureInfo;
- using System.Diagnostics.SymbolStore;
- using System.Reflection;
- using System.Security;
- using System.Collections;
- using System.Security.Permissions;
- using System.Runtime.InteropServices;
-
- [HostProtection(MayLeakOnAbort = true)]
- [ClassInterface(ClassInterfaceType.None)]
- [ComDefaultInterface(typeof(_MethodBuilder))]
- [System.Runtime.InteropServices.ComVisible(true)]
- public sealed class MethodBuilder : MethodInfo, _MethodBuilder
- {
- #region Private Data Members
-
- internal string m_strName;
-
- private MethodToken m_tkMethod;
-
- internal Module m_module;
- internal TypeBuilder m_containingType;
- private MethodBuilder m_link;
-
-
- private int[] m_RVAFixups;
-
- private int[] m_mdMethodFixups;
-
- private SignatureHelper m_localSignature;
- internal LocalSymInfo m_localSymInfo;
-
- internal ILGenerator m_ilGenerator;
- private byte[] m_ubBody;
-
- private int m_numExceptions;
-
- private __ExceptionInstance[] m_exceptions;
-
-
- internal bool m_bIsBaked;
- private bool m_bIsGlobalMethod;
- private bool m_fInitLocals;
-
-
- private MethodAttributes m_iAttributes;
- private CallingConventions m_callingConvention;
- private MethodImplAttributes m_dwMethodImplFlags;
-
-
- private SignatureHelper m_signature;
- internal Type[] m_parameterTypes;
- private ParameterBuilder m_retParam;
- internal Type m_returnType;
- private Type[] m_returnTypeRequiredCustomModifiers;
- private Type[] m_returnTypeOptionalCustomModifiers;
- private Type[][] m_parameterTypeRequiredCustomModifiers;
- private Type[][] m_parameterTypeOptionalCustomModifiers;
-
-
- private GenericTypeParameterBuilder[] m_inst;
- private bool m_bIsGenMethDef;
- #endregion
-
- #region Constructor
- internal MethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module mod, TypeBuilder type, bool bIsGlobalMethod)
- {
- Init(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, mod,
- type, bIsGlobalMethod);
- }
-
- internal MethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers, Module mod,
- TypeBuilder type, bool bIsGlobalMethod)
- {
- Init(name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, mod,
- type, bIsGlobalMethod);
- }
-
- private void Init(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers, Module mod,
- TypeBuilder type, bool bIsGlobalMethod)
- {
- if (name == null)
- throw new ArgumentNullException("name");
-
- if (name.Length == 0)
- throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "name");
-
- if (name[0] == '\0')
- throw new ArgumentException(Environment.GetResourceString("Argument_IllegalName"), "name");
-
- if (mod == null)
- throw new ArgumentNullException("mod");
-
- if (parameterTypes != null) {
- foreach (Type t in parameterTypes) {
- if (t == null)
- throw new ArgumentNullException("parameterTypes");
- }
- }
-
- m_link = type.m_currentMethod;
- type.m_currentMethod = this;
- m_strName = name;
- m_module = mod;
- m_containingType = type;
- m_localSignature = SignatureHelper.GetLocalVarSigHelper(mod);
-
-
-
-
-
-
-
- {
- m_returnType = returnType;
- }
-
- if ((attributes & MethodAttributes.Static) == 0) {
-
- callingConvention = callingConvention | CallingConventions.HasThis;
- }
- else if ((attributes & MethodAttributes.Virtual) != 0) {
-
- throw new ArgumentException(Environment.GetResourceString("Arg_NoStaticVirtual"));
- }
-
- if ((attributes & MethodAttributes.SpecialName) != MethodAttributes.SpecialName) {
- if ((type.Attributes & TypeAttributes.Interface) == TypeAttributes.Interface) {
-
- if ((attributes & (MethodAttributes.Abstract | MethodAttributes.Virtual)) != (MethodAttributes.Abstract | MethodAttributes.Virtual) && (attributes & MethodAttributes.Static) == 0)
- throw new ArgumentException(Environment.GetResourceString("Argument_BadAttributeOnInterfaceMethod"));
- }
- }
-
- m_callingConvention = callingConvention;
-
- if (parameterTypes != null) {
- m_parameterTypes = new Type[parameterTypes.Length];
- Array.Copy(parameterTypes, m_parameterTypes, parameterTypes.Length);
- }
- else {
- m_parameterTypes = null;
- }
-
- m_returnTypeRequiredCustomModifiers = returnTypeRequiredCustomModifiers;
- m_returnTypeOptionalCustomModifiers = returnTypeOptionalCustomModifiers;
- m_parameterTypeRequiredCustomModifiers = parameterTypeRequiredCustomModifiers;
- m_parameterTypeOptionalCustomModifiers = parameterTypeOptionalCustomModifiers;
-
-
-
-
-
- m_iAttributes = attributes;
- m_bIsGlobalMethod = bIsGlobalMethod;
- m_bIsBaked = false;
- m_fInitLocals = true;
-
- m_localSymInfo = new LocalSymInfo();
- m_ubBody = null;
- m_ilGenerator = null;
-
-
- m_dwMethodImplFlags = MethodImplAttributes.IL;
-
-
- }
-
- #endregion
-
- #region Internal Members
- internal void CheckContext(params Type[][] typess)
- {
- ((AssemblyBuilder)Module.Assembly).CheckContext(typess);
- }
- internal void CheckContext(params Type[] types)
- {
- ((AssemblyBuilder)Module.Assembly).CheckContext(types);
- }
-
- internal void CreateMethodBodyHelper(ILGenerator il)
- {
-
-
-
- __ExceptionInfo[] excp;
- int counter = 0;
- int[] filterAddrs;
- int[] catchAddrs;
- int[] catchEndAddrs;
- Type[] catchClass;
- int[] type;
- int numCatch;
- int start;
- int end;
- ModuleBuilder dynMod = (ModuleBuilder)m_module;
-
- m_containingType.ThrowIfCreated();
-
- if (m_bIsBaked) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MethodHasBody"));
- }
-
- if (il == null) {
- throw new ArgumentNullException("il");
- }
-
- if (il.m_methodBuilder != this && il.m_methodBuilder != null) {
-
-
-
-
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadILGeneratorUsage"));
- }
-
- ThrowIfShouldNotHaveBody();
-
- if (il.m_ScopeTree.m_iOpenScopeCount != 0) {
-
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_OpenLocalVariableScope"));
- }
-
-
- m_ubBody = il.BakeByteArray();
-
- m_RVAFixups = il.GetRVAFixups();
- m_mdMethodFixups = il.GetTokenFixups();
-
-
- excp = il.GetExceptions();
- m_numExceptions = CalculateNumberOfExceptions(excp);
- if (m_numExceptions > 0) {
-
- m_exceptions = new __ExceptionInstance[m_numExceptions];
-
- for (int i = 0; i < excp.Length; i++) {
- filterAddrs = excp[i].GetFilterAddresses();
- catchAddrs = excp[i].GetCatchAddresses();
- catchEndAddrs = excp[i].GetCatchEndAddresses();
- catchClass = excp[i].GetCatchClass();
-
-
-
- for (int j = 0; j < catchClass.Length; j++) {
- if (catchClass[j] != null)
- dynMod.GetTypeToken(catchClass[j]);
- }
-
- numCatch = excp[i].GetNumberOfCatches();
- start = excp[i].GetStartAddress();
- end = excp[i].GetEndAddress();
- type = excp[i].GetExceptionTypes();
- for (int j = 0; j < numCatch; j++) {
- int tkExceptionClass = 0;
- if (catchClass[j] != null)
- tkExceptionClass = dynMod.GetTypeToken(catchClass[j]).Token;
- switch (type[j]) {
- case __ExceptionInfo.None:
- case __ExceptionInfo.Fault:
- case __ExceptionInfo.Filter:
- m_exceptions[counter++] = new __ExceptionInstance(start, end, filterAddrs[j], catchAddrs[j], catchEndAddrs[j], type[j], tkExceptionClass);
- break;
- case __ExceptionInfo.Finally:
-
- m_exceptions[counter++] = new __ExceptionInstance(start, excp[i].GetFinallyEndAddress(), filterAddrs[j], catchAddrs[j], catchEndAddrs[j], type[j], tkExceptionClass);
- break;
- }
- }
-
- }
- }
-
-
- m_bIsBaked = true;
-
- if (dynMod.GetSymWriter() != null) {
-
-
-
-
- SymbolToken tk = new SymbolToken(MetadataTokenInternal);
- ISymbolWriter symWriter = dynMod.GetSymWriter();
-
-
- symWriter.OpenMethod(tk);
-
-
-
-
- symWriter.OpenScope(0);
-
- if (m_symCustomAttrs != null) {
- foreach (SymCustomAttr symCustomAttr in m_symCustomAttrs)
- dynMod.GetSymWriter().SetSymAttribute(new SymbolToken(MetadataTokenInternal), symCustomAttr.m_name, symCustomAttr.m_data);
- }
-
- if (m_localSymInfo != null)
- m_localSymInfo.EmitLocalSymInfo(symWriter);
- il.m_ScopeTree.EmitScopeTree(symWriter);
- il.m_LineNumberInfo.EmitLineNumberInfo(symWriter);
- symWriter.CloseScope(il.m_length);
- symWriter.CloseMethod();
- }
- }
-
- internal override Type[] GetParameterTypes()
- {
- if (m_parameterTypes == null)
- m_parameterTypes = new Type[0];
-
- return m_parameterTypes;
- }
-
- internal void SetToken(MethodToken token)
- {
- m_tkMethod = token;
- }
-
- internal byte[] GetBody()
- {
-
-
- return m_ubBody;
- }
-
- internal int[] GetTokenFixups()
- {
- return m_mdMethodFixups;
- }
-
- internal int[] GetRVAFixups()
- {
- return m_RVAFixups;
- }
-
- internal SignatureHelper GetMethodSignature()
- {
- if (m_parameterTypes == null)
- m_parameterTypes = new Type[0];
-
- m_signature = SignatureHelper.GetMethodSigHelper(m_module, m_callingConvention, m_inst != null ? m_inst.Length : 0, m_returnType == null ? typeof(void) : m_returnType, m_returnTypeRequiredCustomModifiers, m_returnTypeOptionalCustomModifiers, m_parameterTypes, m_parameterTypeRequiredCustomModifiers, m_parameterTypeOptionalCustomModifiers);
-
- return m_signature;
- }
-
- internal SignatureHelper GetLocalsSignature()
- {
- if (m_ilGenerator != null) {
- if (m_ilGenerator.m_localCount != 0) {
-
- return m_ilGenerator.m_localSignature;
- }
- }
-
- return m_localSignature;
- }
-
- internal int GetNumberOfExceptions()
- {
- return m_numExceptions;
- }
-
- internal __ExceptionInstance[] GetExceptionInstances()
- {
- return m_exceptions;
- }
-
- internal int CalculateNumberOfExceptions(__ExceptionInfo[] excp)
- {
- int num = 0;
-
- if (excp == null) {
- return 0;
- }
-
- for (int i = 0; i < excp.Length; i++) {
- num += excp[i].GetNumberOfCatches();
- }
-
- return num;
- }
-
- internal bool IsTypeCreated()
- {
- return (m_containingType != null && m_containingType.m_hasBeenCreated);
- }
-
- internal TypeBuilder GetTypeBuilder()
- {
- return m_containingType;
- }
- #endregion
-
- #region Object Overrides
- public override bool Equals(object obj)
- {
- if (!(obj is MethodBuilder)) {
- return false;
- }
- if (!(this.m_strName.Equals(((MethodBuilder)obj).m_strName))) {
- return false;
- }
-
- if (m_iAttributes != (((MethodBuilder)obj).m_iAttributes)) {
- return false;
- }
-
- SignatureHelper thatSig = ((MethodBuilder)obj).GetMethodSignature();
- if (thatSig.Equals(GetMethodSignature())) {
- return true;
- }
- return false;
- }
-
- public override int GetHashCode()
- {
- return this.m_strName.GetHashCode();
- }
-
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder(1000);
- sb.Append("Name: " + m_strName + " " + Environment.NewLine);
- sb.Append("Attributes: " + (int)m_iAttributes + Environment.NewLine);
- sb.Append("Method Signature: " + GetMethodSignature() + Environment.NewLine);
-
- sb.Append(Environment.NewLine);
- return sb.ToString();
- }
-
- #endregion
-
- #region MemberInfo Overrides
- public override string Name {
- get { return m_strName; }
- }
-
- internal override int MetadataTokenInternal {
- get { return GetToken().Token; }
- }
-
- public override Module Module {
- get { return m_containingType.Module; }
- }
-
- public override Type DeclaringType {
- get {
- if (m_containingType.m_isHiddenGlobalType == true)
- return null;
- return m_containingType;
- }
- }
-
- public override ICustomAttributeProvider ReturnTypeCustomAttributes {
- get { return null; }
- }
-
- public override Type ReflectedType {
- get {
- if (m_containingType.m_isHiddenGlobalType == true)
- return null;
- return m_containingType;
- }
- }
-
- #endregion
-
- #region MethodBase Overrides
- public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
- {
- throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
- }
-
- public override MethodImplAttributes GetMethodImplementationFlags()
- {
- return m_dwMethodImplFlags;
- }
-
- public override MethodAttributes Attributes {
- get { return m_iAttributes; }
- }
-
- public override CallingConventions CallingConvention {
- get { return m_callingConvention; }
- }
-
- public override RuntimeMethodHandle MethodHandle {
- get {
- throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
- }
- }
-
- #endregion
-
- #region MethodInfo Overrides
- public override MethodInfo GetBaseDefinition()
- {
- return this;
- }
-
- internal override Type GetReturnType()
- {
- return m_returnType;
- }
-
- public override ParameterInfo[] GetParameters()
- {
- if (!m_bIsBaked)
- throw new NotSupportedException(Environment.GetResourceString("InvalidOperation_TypeNotCreated"));
-
- Type rti = m_containingType.m_runtimeType;
- MethodInfo rmi = rti.GetMethod(m_strName, m_parameterTypes);
-
- return rmi.GetParameters();
- }
-
- public override ParameterInfo ReturnParameter {
- get {
- if (!m_bIsBaked)
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_TypeNotCreated"));
-
- Type rti = m_containingType.m_runtimeType;
- MethodInfo rmi = rti.GetMethod(m_strName, m_parameterTypes);
-
- return rmi.ReturnParameter;
- }
- }
- #endregion
-
- #region ICustomAttributeProvider Implementation
- public override object[] GetCustomAttributes(bool inherit)
- {
-
- throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
- }
-
- public override object[] GetCustomAttributes(Type attributeType, bool inherit)
- {
-
- throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
- }
-
- public override bool IsDefined(Type attributeType, bool inherit)
- {
-
- throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
- }
-
- #endregion
-
- #region Generic Members
- public override bool IsGenericMethodDefinition {
- get { return m_bIsGenMethDef; }
- }
-
- public override bool ContainsGenericParameters {
- get {
- throw new NotSupportedException();
- }
- }
-
- public override MethodInfo GetGenericMethodDefinition()
- {
- if (!IsGenericMethod)
- throw new InvalidOperationException();
- return this;
- }
-
- public override bool IsGenericMethod {
- get { return m_inst != null; }
- }
-
- public override Type[] GetGenericArguments()
- {
- return m_inst;
- }
-
- public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
- {
- return new MethodBuilderInstantiation(this, typeArguments);
- }
-
-
- public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names)
- {
- if (m_inst != null)
- throw new InvalidOperationException();
-
- if (names == null)
- throw new ArgumentNullException("names");
-
- for (int i = 0; i < names.Length; i++)
- if (names[i] == null)
- throw new ArgumentNullException("names");
-
- if (m_tkMethod.Token != 0)
- throw new InvalidOperationException();
-
- if (names.Length == 0)
- throw new ArgumentException();
-
- m_bIsGenMethDef = true;
- m_inst = new GenericTypeParameterBuilder[names.Length];
- for (int i = 0; i < names.Length; i++)
- m_inst[i] = new GenericTypeParameterBuilder(new TypeBuilder(names[i], i, this));
-
- return m_inst;
- }
-
- internal void ThrowIfGeneric()
- {
- if (IsGenericMethod && !IsGenericMethodDefinition)
- throw new InvalidOperationException();
- }
- #endregion
-
- #region Public Members
- public MethodToken GetToken()
- {
- if (m_tkMethod.Token == 0) {
- if (m_link != null)
- m_link.GetToken();
-
- int sigLength;
- byte[] sigBytes = GetMethodSignature().InternalGetSignature(out sigLength);
-
- m_tkMethod = new MethodToken(TypeBuilder.InternalDefineMethod(m_containingType.MetadataTokenInternal, m_strName, sigBytes, sigLength, Attributes, m_module));
-
- if (m_inst != null)
- foreach (GenericTypeParameterBuilder tb in m_inst)
- if (!tb.m_type.IsCreated())
- tb.m_type.CreateType();
-
- TypeBuilder.InternalSetMethodImpl(m_module, MetadataTokenInternal, m_dwMethodImplFlags);
- }
-
- return m_tkMethod;
- }
-
- public void SetParameters(params Type[] parameterTypes)
- {
- CheckContext(parameterTypes);
- SetSignature(null, null, null, parameterTypes, null, null);
- }
-
- public void SetReturnType(Type returnType)
- {
- CheckContext(returnType);
- SetSignature(returnType, null, null, null, null, null);
- }
-
- public void SetSignature(Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
- {
- CheckContext(returnType);
- CheckContext(returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes);
- CheckContext(parameterTypeRequiredCustomModifiers);
- CheckContext(parameterTypeOptionalCustomModifiers);
-
- ThrowIfGeneric();
-
- if (returnType != null) {
- m_returnType = returnType;
- }
-
- if (parameterTypes != null) {
- m_parameterTypes = new Type[parameterTypes.Length];
- Array.Copy(parameterTypes, m_parameterTypes, parameterTypes.Length);
- }
-
- m_returnTypeRequiredCustomModifiers = returnTypeRequiredCustomModifiers;
- m_returnTypeOptionalCustomModifiers = returnTypeOptionalCustomModifiers;
- m_parameterTypeRequiredCustomModifiers = parameterTypeRequiredCustomModifiers;
- m_parameterTypeOptionalCustomModifiers = parameterTypeOptionalCustomModifiers;
- }
-
-
- public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string strParamName)
- {
- ThrowIfGeneric();
- m_containingType.ThrowIfCreated();
-
- if (position < 0)
- throw new ArgumentOutOfRangeException(Environment.GetResourceString("ArgumentOutOfRange_ParamSequence"));
-
- if (position > 0 && (m_parameterTypes == null || position > m_parameterTypes.Length))
- throw new ArgumentOutOfRangeException(Environment.GetResourceString("ArgumentOutOfRange_ParamSequence"));
-
- attributes = attributes & ~ParameterAttributes.ReservedMask;
- return new ParameterBuilder(this, position, attributes, strParamName);
- }
-
- [Obsolete("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")]
- public void SetMarshal(UnmanagedMarshal unmanagedMarshal)
- {
- ThrowIfGeneric();
-
-
-
- m_containingType.ThrowIfCreated();
-
- if (m_retParam == null) {
- m_retParam = new ParameterBuilder(this, 0, 0, null);
- }
-
- m_retParam.SetMarshal(unmanagedMarshal);
- }
-
- private ArrayList m_symCustomAttrs;
- private struct SymCustomAttr
- {
- public SymCustomAttr(string name, byte[] data)
- {
- m_name = name;
- m_data = data;
- }
- public string m_name;
- public byte[] m_data;
- }
-
- public void SetSymCustomAttribute(string name, byte[] data)
- {
- ThrowIfGeneric();
-
-
- m_containingType.ThrowIfCreated();
-
- ModuleBuilder dynMod = (ModuleBuilder)m_module;
- if (dynMod.GetSymWriter() == null) {
-
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotADebugModule"));
- }
-
- if (m_symCustomAttrs == null)
- m_symCustomAttrs = new ArrayList();
-
- m_symCustomAttrs.Add(new SymCustomAttr(name, data));
- }
-
- public void AddDeclarativeSecurity(SecurityAction action, PermissionSet pset)
- {
- ThrowIfGeneric();
-
- if (pset == null)
- throw new ArgumentNullException("pset");
-
- if (!Enum.IsDefined(typeof(SecurityAction), action) || action == SecurityAction.RequestMinimum || action == SecurityAction.RequestOptional || action == SecurityAction.RequestRefuse)
- throw new ArgumentOutOfRangeException("action");
-
-
- m_containingType.ThrowIfCreated();
-
-
- byte[] blob = null;
- if (!pset.IsEmpty())
- blob = pset.EncodeXml();
-
-
- TypeBuilder.InternalAddDeclarativeSecurity(m_module, MetadataTokenInternal, action, blob);
- }
-
- public void CreateMethodBody(byte[] il, int count)
- {
- ThrowIfGeneric();
-
-
-
-
- if (m_bIsBaked) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MethodBaked"));
- }
-
- m_containingType.ThrowIfCreated();
-
- if (il != null && (count < 0 || count > il.Length))
- throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Index"));
- if (il == null) {
- m_ubBody = null;
- return;
- }
- m_ubBody = new byte[count];
- Array.Copy(il, m_ubBody, count);
- m_bIsBaked = true;
- }
-
-