mscorlib(4.0.0.0) API with additions
ExceptionHandler.cs
2 
4 {
6  [ComVisible(false)]
7  public struct ExceptionHandler : IEquatable<ExceptionHandler>
8  {
9  internal readonly int m_exceptionClass;
10 
11  internal readonly int m_tryStartOffset;
12 
13  internal readonly int m_tryEndOffset;
14 
15  internal readonly int m_filterOffset;
16 
17  internal readonly int m_handlerStartOffset;
18 
19  internal readonly int m_handlerEndOffset;
20 
21  internal readonly ExceptionHandlingClauseOptions m_kind;
22 
25  public int ExceptionTypeToken => m_exceptionClass;
26 
29  public int TryOffset => m_tryStartOffset;
30 
33  public int TryLength => m_tryEndOffset - m_tryStartOffset;
34 
37  public int FilterOffset => m_filterOffset;
38 
41  public int HandlerOffset => m_handlerStartOffset;
42 
45  public int HandlerLength => m_handlerEndOffset - m_handlerStartOffset;
46 
50 
61  public ExceptionHandler(int tryOffset, int tryLength, int filterOffset, int handlerOffset, int handlerLength, ExceptionHandlingClauseOptions kind, int exceptionTypeToken)
62  {
63  if (tryOffset < 0)
64  {
65  throw new ArgumentOutOfRangeException("tryOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
66  }
67  if (tryLength < 0)
68  {
69  throw new ArgumentOutOfRangeException("tryLength", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
70  }
71  if (filterOffset < 0)
72  {
73  throw new ArgumentOutOfRangeException("filterOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
74  }
75  if (handlerOffset < 0)
76  {
77  throw new ArgumentOutOfRangeException("handlerOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
78  }
79  if (handlerLength < 0)
80  {
81  throw new ArgumentOutOfRangeException("handlerLength", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
82  }
83  if ((long)tryOffset + (long)tryLength > int.MaxValue)
84  {
85  throw new ArgumentOutOfRangeException("tryLength", Environment.GetResourceString("ArgumentOutOfRange_Range", 0, int.MaxValue - tryOffset));
86  }
87  if ((long)handlerOffset + (long)handlerLength > int.MaxValue)
88  {
89  throw new ArgumentOutOfRangeException("handlerLength", Environment.GetResourceString("ArgumentOutOfRange_Range", 0, int.MaxValue - handlerOffset));
90  }
91  if (kind == ExceptionHandlingClauseOptions.Clause && (exceptionTypeToken & 0xFFFFFF) == 0)
92  {
93  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeToken", exceptionTypeToken), "exceptionTypeToken");
94  }
95  if (!IsValidKind(kind))
96  {
97  throw new ArgumentOutOfRangeException("kind", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
98  }
99  m_tryStartOffset = tryOffset;
100  m_tryEndOffset = tryOffset + tryLength;
101  m_filterOffset = filterOffset;
102  m_handlerStartOffset = handlerOffset;
103  m_handlerEndOffset = handlerOffset + handlerLength;
104  m_kind = kind;
105  m_exceptionClass = exceptionTypeToken;
106  }
107 
108  internal ExceptionHandler(int tryStartOffset, int tryEndOffset, int filterOffset, int handlerStartOffset, int handlerEndOffset, int kind, int exceptionTypeToken)
109  {
110  m_tryStartOffset = tryStartOffset;
111  m_tryEndOffset = tryEndOffset;
112  m_filterOffset = filterOffset;
113  m_handlerStartOffset = handlerStartOffset;
114  m_handlerEndOffset = handlerEndOffset;
115  m_kind = (ExceptionHandlingClauseOptions)kind;
116  m_exceptionClass = exceptionTypeToken;
117  }
118 
119  private static bool IsValidKind(ExceptionHandlingClauseOptions kind)
120  {
121  switch (kind)
122  {
123  case ExceptionHandlingClauseOptions.Clause:
124  case ExceptionHandlingClauseOptions.Filter:
125  case ExceptionHandlingClauseOptions.Finally:
127  return true;
128  default:
129  return false;
130  }
131  }
132 
135  public override int GetHashCode()
136  {
137  return m_exceptionClass ^ m_tryStartOffset ^ m_tryEndOffset ^ m_filterOffset ^ m_handlerStartOffset ^ m_handlerEndOffset ^ (int)m_kind;
138  }
139 
144  public override bool Equals(object obj)
145  {
146  if (obj is ExceptionHandler)
147  {
148  return Equals((ExceptionHandler)obj);
149  }
150  return false;
151  }
152 
157  public bool Equals(ExceptionHandler other)
158  {
159  if (other.m_exceptionClass == m_exceptionClass && other.m_tryStartOffset == m_tryStartOffset && other.m_tryEndOffset == m_tryEndOffset && other.m_filterOffset == m_filterOffset && other.m_handlerStartOffset == m_handlerStartOffset && other.m_handlerEndOffset == m_handlerEndOffset)
160  {
161  return other.m_kind == m_kind;
162  }
163  return false;
164  }
165 
171  public static bool operator ==(ExceptionHandler left, ExceptionHandler right)
172  {
173  return left.Equals(right);
174  }
175 
181  public static bool operator !=(ExceptionHandler left, ExceptionHandler right)
182  {
183  return !left.Equals(right);
184  }
185  }
186 }
static bool operator==(ExceptionHandler left, ExceptionHandler right)
Determines whether two specified instances of T:System.Reflection.Emit.ExceptionHandler are equal.
override bool Equals(object obj)
Indicates whether this instance of the T:System.Reflection.Emit.ExceptionHandler object is equal to a...
int FilterOffset
Gets the byte offset at which the filter code for the exception handler begins.
override int GetHashCode()
Serves as the default hash function.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
int TryLength
Gets the length, in bytes, of the code protected by this exception handler.
int HandlerLength
Gets the length, in bytes, of the exception handler.
int HandlerOffset
Gets the byte offset of the first instruction of the exception handler.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
ExceptionHandler(int tryOffset, int tryLength, int filterOffset, int handlerOffset, int handlerLength, ExceptionHandlingClauseOptions kind, int exceptionTypeToken)
Initializes a new instance of the T:System.Reflection.Emit.ExceptionHandler class with the specified ...
ExceptionHandlingClauseOptions Kind
Gets a value that represents the kind of exception handler this object represents.
int ExceptionTypeToken
Gets the token of the exception type handled by this handler.
int TryOffset
Gets the byte offset at which the code that is protected by this exception handler begins.
static bool operator !=(ExceptionHandler left, ExceptionHandler right)
Determines whether two specified instances of T:System.Reflection.Emit.ExceptionHandler are not equal...
bool Equals(ExceptionHandler other)
Indicates whether this instance of the T:System.Reflection.Emit.ExceptionHandler object is equal to a...
The exception that is thrown when one of the arguments provided to a method is not valid.
Represents an exception handler in a byte array of IL to be passed to a method such as M:System....
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: IEquatable.cs:6
ExceptionHandlingClauseOptions
Identifies kinds of exception-handling clauses.