The Labs \ Source Viewer \ SSCLI \ System.Reflection \ MethodBody

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. using System;
  16. using System.Globalization;
  17. using System.Collections.Generic;
  18. namespace System.Reflection
  19. {
  20.     [Flags()]
  21.     [System.Runtime.InteropServices.ComVisible(true)]
  22.     public enum ExceptionHandlingClauseOptions : int
  23.     {
  24.         Clause = 0,
  25.         Filter = 1,
  26.         Finally = 2,
  27.         Fault = 4
  28.     }
  29.    
  30.     [System.Runtime.InteropServices.ComVisible(true)]
  31.     public sealed class ExceptionHandlingClause
  32.     {
  33.         #region Private costructor
  34.         // This class can only be created from inside the EE.
  35.         private ExceptionHandlingClause()
  36.         {
  37.         }
  38.         #endregion
  39.        
  40.         #region Private Data Members
  41.         private MethodBody m_methodBody;
  42.         private ExceptionHandlingClauseOptions m_flags;
  43.         private int m_tryOffset;
  44.         private int m_tryLength;
  45.         private int m_handlerOffset;
  46.         private int m_handlerLength;
  47.         private int m_catchMetadataToken;
  48.         private int m_filterOffset;
  49.         #endregion
  50.        
  51.         #region Public Members
  52.         public ExceptionHandlingClauseOptions Flags {
  53.             get { return m_flags; }
  54.         }
  55.         public int TryOffset {
  56.             get { return m_tryOffset; }
  57.         }
  58.         public int TryLength {
  59.             get { return m_tryLength; }
  60.         }
  61.         public int HandlerOffset {
  62.             get { return m_handlerOffset; }
  63.         }
  64.         public int HandlerLength {
  65.             get { return m_handlerLength; }
  66.         }
  67.        
  68.         public int FilterOffset {
  69.             get {
  70.                 if (m_flags != ExceptionHandlingClauseOptions.Filter)
  71.                     throw new InvalidOperationException(Environment.GetResourceString("Arg_EHClauseNotFilter"));
  72.                
  73.                 return m_filterOffset;
  74.             }
  75.         }
  76.        
  77.         public Type CatchType {
  78.             get {
  79.                 if (m_flags != ExceptionHandlingClauseOptions.Clause)
  80.                     throw new InvalidOperationException(Environment.GetResourceString("Arg_EHClauseNotClause"));
  81.                
  82.                 Type type = null;
  83.                
  84.                 if (!MetadataToken.IsNullToken(m_catchMetadataToken)) {
  85.                     Type declaringType = m_methodBody.m_methodBase.DeclaringType;
  86.                     Module module = (declaringType == null) ? m_methodBody.m_methodBase.Module : declaringType.Module;
  87.                     type = module.ResolveType(m_catchMetadataToken, (declaringType == null) ? null : declaringType.GetGenericArguments(), m_methodBody.m_methodBase is MethodInfo ? m_methodBody.m_methodBase.GetGenericArguments() : null);
  88.                 }
  89.                
  90.                 return type;
  91.             }
  92.         }
  93.         #endregion
  94.        
  95.         #region Object Overrides
  96.         public override string ToString()
  97.         {
  98.             if (Flags == ExceptionHandlingClauseOptions.Clause) {
  99.                 return String.Format(CultureInfo.CurrentUICulture, "Flags={0}, TryOffset={1}, TryLength={2}, HandlerOffset={3}, HandlerLength={4}, CatchType={5}", Flags, TryOffset, TryLength, HandlerOffset, HandlerLength, CatchType);
  100.             }
  101.            
  102.             if (Flags == ExceptionHandlingClauseOptions.Filter) {
  103.                 return String.Format(CultureInfo.CurrentUICulture, "Flags={0}, TryOffset={1}, TryLength={2}, HandlerOffset={3}, HandlerLength={4}, FilterOffset={5}", Flags, TryOffset, TryLength, HandlerOffset, HandlerLength, FilterOffset);
  104.             }
  105.            
  106.             return String.Format(CultureInfo.CurrentUICulture, "Flags={0}, TryOffset={1}, TryLength={2}, HandlerOffset={3}, HandlerLength={4}", Flags, TryOffset, TryLength, HandlerOffset, HandlerLength);
  107.            
  108.         }
  109.         #endregion
  110.     }
  111.    
  112.     [System.Runtime.InteropServices.ComVisible(true)]
  113.     public sealed class MethodBody
  114.     {
  115.         #region Private costructor
  116.         // This class can only be created from inside the EE.
  117.         private MethodBody()
  118.         {
  119.         }
  120.         #endregion
  121.        
  122.         #region Private Data Members
  123.         private byte[] m_IL;
  124.         private ExceptionHandlingClause[] m_exceptionHandlingClauses;
  125.         private LocalVariableInfo[] m_localVariables;
  126.         internal MethodBase m_methodBase;
  127.         private int m_localSignatureMetadataToken;
  128.         private int m_maxStackSize;
  129.         private bool m_initLocals;
  130.         #endregion
  131.        
  132.         #region Public Members
  133.         public int LocalSignatureMetadataToken {
  134.             get { return m_localSignatureMetadataToken; }
  135.         }
  136.         public IList<LocalVariableInfo> LocalVariables {
  137.             get { return Array.AsReadOnly(m_localVariables); }
  138.         }
  139.         public int MaxStackSize {
  140.             get { return m_maxStackSize; }
  141.         }
  142.         public bool InitLocals {
  143.             get { return m_initLocals; }
  144.         }
  145.         public byte[] GetILAsByteArray()
  146.         {
  147.             return m_IL;
  148.         }
  149.         public IList<ExceptionHandlingClause> ExceptionHandlingClauses {
  150.             get { return Array.AsReadOnly(m_exceptionHandlingClauses); }
  151.         }
  152.         #endregion
  153.     }
  154.    
  155.     [System.Runtime.InteropServices.ComVisible(true)]
  156.     public class LocalVariableInfo
  157.     {
  158.         #region Private Data Members
  159.         private int m_isPinned;
  160.         private int m_localIndex;
  161.         private RuntimeTypeHandle m_typeHandle;
  162.         #endregion
  163.        
  164.         #region Constructor
  165.         internal LocalVariableInfo()
  166.         {
  167.         }
  168.         #endregion
  169.        
  170.         #region Object Overrides
  171.         public override string ToString()
  172.         {
  173.             string toString = LocalType.ToString() + " (" + LocalIndex + ")";
  174.            
  175.             if (IsPinned)
  176.                 toString += " (pinned)";
  177.            
  178.             return toString;
  179.         }
  180.         #endregion
  181.        
  182.         #region Public Members
  183.         public virtual Type LocalType {
  184.             get {
  185.                 BCLDebug.Assert(m_typeHandle != null, "type handle must be set!");
  186.                 return m_typeHandle.GetRuntimeType();
  187.             }
  188.         }
  189.         public virtual bool IsPinned {
  190.             get { return m_isPinned != 0; }
  191.         }
  192.         public virtual int LocalIndex {
  193.             get { return m_localIndex; }
  194.         }
  195.         #endregion
  196.     }
  197. }

Developer Fusion