|
5 | 5 | using System.IO;
|
6 | 6 | using System.Reflection;
|
7 | 7 |
|
8 |
| - internal class TestContextAccessor |
| 8 | + internal partial class TestContextAccessor |
9 | 9 | {
|
10 |
| - public static TestContextAccessor GetTestContext() |
| 10 | + private static readonly object s_Lock = new object(); |
| 11 | + private static TestContextAccessor s_TestContext; |
| 12 | + |
| 13 | + public static TestContextAccessor Instance |
| 14 | + { |
| 15 | + get |
| 16 | + { |
| 17 | + if (s_TestContext == null) { |
| 18 | + lock (s_Lock) { |
| 19 | + if (s_TestContext == null) { |
| 20 | + s_TestContext = Create(); |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + return s_TestContext; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private static TestContextAccessor Create() |
11 | 29 | {
|
12 | 30 | Assembly nUnitAssembly;
|
13 | 31 |
|
@@ -102,6 +120,8 @@ private TestContextAccessor(Assembly nUnitAssembly)
|
102 | 120 | }
|
103 | 121 | }
|
104 | 122 | }
|
| 123 | + |
| 124 | + InitWriteAccessor(currentContext); |
105 | 125 | }
|
106 | 126 |
|
107 | 127 | private static PrivateObject GetCurrentContext(Assembly nUnitAssembly)
|
@@ -131,5 +151,197 @@ private TestAccessor Test
|
131 | 151 | public string TestName { get { return Test.Name; } }
|
132 | 152 |
|
133 | 153 | public string TestFullName { get { return Test.FullName; } }
|
| 154 | + |
| 155 | + #region Write via Reflection |
| 156 | + private Action<ulong> m_WriteUlong; |
| 157 | + private Action<uint> m_WriteUint; |
| 158 | + private Action<string> m_WriteString; |
| 159 | + private Action<float> m_WriteFloat; |
| 160 | + private Action<object> m_WriteObject; |
| 161 | + private Action<decimal> m_WriteDecimal; |
| 162 | + private Action<long> m_WriteLong; |
| 163 | + private Action<int> m_WriteInt; |
| 164 | + private Action<double> m_WriteDouble; |
| 165 | + private Action<char[]> m_WriteCharArray; |
| 166 | + private Action<char> m_WriteChar; |
| 167 | + private Action<bool> m_WriteBool; |
| 168 | + private Action<string, object> m_WriteFormat1; |
| 169 | + private Action<string, object, object> m_WriteFormat2; |
| 170 | + private Action<string, object, object, object> m_WriteFormat3; |
| 171 | + private Action<string, object[]> m_WriteFormatS; |
| 172 | + |
| 173 | + private Action m_WriteLine; |
| 174 | + private Action<ulong> m_WriteLineUlong; |
| 175 | + private Action<uint> m_WriteLineUint; |
| 176 | + private Action<string> m_WriteLineString; |
| 177 | + private Action<float> m_WriteLineFloat; |
| 178 | + private Action<object> m_WriteLineObject; |
| 179 | + private Action<decimal> m_WriteLineDecimal; |
| 180 | + private Action<long> m_WriteLineLong; |
| 181 | + private Action<int> m_WriteLineInt; |
| 182 | + private Action<double> m_WriteLineDouble; |
| 183 | + private Action<char[]> m_WriteLineCharArray; |
| 184 | + private Action<char> m_WriteLineChar; |
| 185 | + private Action<bool> m_WriteLineBool; |
| 186 | + private Action<string, object> m_WriteLineFormat1; |
| 187 | + private Action<string, object, object> m_WriteLineFormat2; |
| 188 | + private Action<string, object, object, object> m_WriteLineFormat3; |
| 189 | + private Action<string, object[]> m_WriteLineFormatS; |
| 190 | + |
| 191 | + private void InitWriteAccessor(PrivateObject currentContext) |
| 192 | + { |
| 193 | + m_WriteUlong = GetDelegate<ulong>(currentContext.RealType, nameof(Write)); |
| 194 | + m_WriteUint = GetDelegate<uint>(currentContext.RealType, nameof(Write)); |
| 195 | + m_WriteString = GetDelegate<string>(currentContext.RealType, nameof(Write)); |
| 196 | + m_WriteFloat = GetDelegate<float>(currentContext.RealType, nameof(Write)); |
| 197 | + m_WriteObject = GetDelegate<object>(currentContext.RealType, nameof(Write)); |
| 198 | + m_WriteDecimal = GetDelegate<decimal>(currentContext.RealType, nameof(Write)); |
| 199 | + m_WriteLong = GetDelegate<long>(currentContext.RealType, nameof(Write)); |
| 200 | + m_WriteInt = GetDelegate<int>(currentContext.RealType, nameof(Write)); |
| 201 | + m_WriteDouble = GetDelegate<double>(currentContext.RealType, nameof(Write)); |
| 202 | + m_WriteCharArray = GetDelegate<char[]>(currentContext.RealType, nameof(Write)); |
| 203 | + m_WriteChar = GetDelegate<char>(currentContext.RealType, nameof(Write)); |
| 204 | + m_WriteBool = GetDelegate<bool>(currentContext.RealType, nameof(Write)); |
| 205 | + |
| 206 | + MethodInfo methodInfoArgs1 = currentContext.RealType.GetMethod(nameof(Write), new Type[] { typeof(string), typeof(object) }); |
| 207 | + if (methodInfoArgs1 == null) |
| 208 | + methodInfoArgs1 = typeof(WriteConsole).GetMethod(nameof(Write), new Type[] { typeof(string), typeof(object) }); |
| 209 | + m_WriteFormat1 = (Action<string, object>)Delegate.CreateDelegate(typeof(Action<string, object>), methodInfoArgs1); |
| 210 | + |
| 211 | + MethodInfo methodInfoArgs2 = currentContext.RealType.GetMethod(nameof(Write), new Type[] { typeof(string), typeof(object), typeof(object) }); |
| 212 | + if (methodInfoArgs2 == null) |
| 213 | + methodInfoArgs2 = typeof(WriteConsole).GetMethod(nameof(Write), new Type[] { typeof(string), typeof(object), typeof(object) }); |
| 214 | + m_WriteFormat2 = (Action<string, object, object>)Delegate.CreateDelegate(typeof(Action<string, object, object>), methodInfoArgs2); |
| 215 | + |
| 216 | + MethodInfo methodInfoArgs3 = currentContext.RealType.GetMethod(nameof(Write), new Type[] { typeof(string), typeof(object), typeof(object), typeof(object) }); |
| 217 | + if (methodInfoArgs3 == null) |
| 218 | + methodInfoArgs3 = typeof(WriteConsole).GetMethod(nameof(Write), new Type[] { typeof(string), typeof(object), typeof(object), typeof(object) }); |
| 219 | + m_WriteFormat3 = (Action<string, object, object, object>)Delegate.CreateDelegate(typeof(Action<string, object, object, object>), methodInfoArgs3); |
| 220 | + |
| 221 | + MethodInfo methodInfoArgsS = currentContext.RealType.GetMethod(nameof(Write), new Type[] { typeof(string), typeof(object[]) }); |
| 222 | + if (methodInfoArgsS == null) |
| 223 | + methodInfoArgsS = typeof(WriteConsole).GetMethod(nameof(Write), new Type[] { typeof(string), typeof(object[]) }); |
| 224 | + m_WriteFormatS = (Action<string, object[]>)Delegate.CreateDelegate(typeof(Action<string, object[]>), methodInfoArgs1); |
| 225 | + |
| 226 | + m_WriteLineUlong = GetDelegate<ulong>(currentContext.RealType, nameof(WriteLine)); |
| 227 | + m_WriteLineUint = GetDelegate<uint>(currentContext.RealType, nameof(WriteLine)); |
| 228 | + m_WriteLineString = GetDelegate<string>(currentContext.RealType, nameof(WriteLine)); |
| 229 | + m_WriteLineFloat = GetDelegate<float>(currentContext.RealType, nameof(WriteLine)); |
| 230 | + m_WriteLineObject = GetDelegate<object>(currentContext.RealType, nameof(WriteLine)); |
| 231 | + m_WriteLineDecimal = GetDelegate<decimal>(currentContext.RealType, nameof(WriteLine)); |
| 232 | + m_WriteLineLong = GetDelegate<long>(currentContext.RealType, nameof(WriteLine)); |
| 233 | + m_WriteLineInt = GetDelegate<int>(currentContext.RealType, nameof(WriteLine)); |
| 234 | + m_WriteLineDouble = GetDelegate<double>(currentContext.RealType, nameof(WriteLine)); |
| 235 | + m_WriteLineCharArray = GetDelegate<char[]>(currentContext.RealType, nameof(WriteLine)); |
| 236 | + m_WriteLineChar = GetDelegate<char>(currentContext.RealType, nameof(WriteLine)); |
| 237 | + m_WriteLineBool = GetDelegate<bool>(currentContext.RealType, nameof(WriteLine)); |
| 238 | + |
| 239 | + MethodInfo methodInfoLineArgs1 = currentContext.RealType.GetMethod(nameof(WriteLine), new Type[] { typeof(string), typeof(object) }); |
| 240 | + if (methodInfoLineArgs1 == null) |
| 241 | + methodInfoLineArgs1 = typeof(WriteConsole).GetMethod(nameof(WriteLine), new Type[] { typeof(string), typeof(object) }); |
| 242 | + m_WriteLineFormat1 = (Action<string, object>)Delegate.CreateDelegate(typeof(Action<string, object>), methodInfoLineArgs1); |
| 243 | + |
| 244 | + MethodInfo methodInfoLineArgs2 = currentContext.RealType.GetMethod(nameof(WriteLine), new Type[] { typeof(string), typeof(object), typeof(object) }); |
| 245 | + if (methodInfoLineArgs2 == null) |
| 246 | + methodInfoLineArgs2 = typeof(WriteConsole).GetMethod(nameof(WriteLine), new Type[] { typeof(string), typeof(object), typeof(object) }); |
| 247 | + m_WriteLineFormat2 = (Action<string, object, object>)Delegate.CreateDelegate(typeof(Action<string, object, object>), methodInfoLineArgs2); |
| 248 | + |
| 249 | + MethodInfo methodInfoLineArgs3 = currentContext.RealType.GetMethod(nameof(WriteLine), new Type[] { typeof(string), typeof(object), typeof(object), typeof(object) }); |
| 250 | + if (methodInfoLineArgs3 == null) |
| 251 | + methodInfoLineArgs3 = typeof(WriteConsole).GetMethod(nameof(WriteLine), new Type[] { typeof(string), typeof(object), typeof(object), typeof(object) }); |
| 252 | + m_WriteLineFormat3 = (Action<string, object, object, object>)Delegate.CreateDelegate(typeof(Action<string, object, object, object>), methodInfoLineArgs3); |
| 253 | + |
| 254 | + MethodInfo methodInfoLineArgsS = currentContext.RealType.GetMethod(nameof(WriteLine), new Type[] { typeof(string), typeof(object[]) }); |
| 255 | + if (methodInfoLineArgsS == null) |
| 256 | + methodInfoLineArgsS = typeof(WriteConsole).GetMethod(nameof(WriteLine), new Type[] { typeof(string), typeof(object[]) }); |
| 257 | + m_WriteLineFormatS = (Action<string, object[]>)Delegate.CreateDelegate(typeof(Action<string, object[]>), methodInfoLineArgs1); |
| 258 | + |
| 259 | +#if NETFRAMEWORK |
| 260 | + MethodInfo methodInfoLine = currentContext.RealType.GetMethod(nameof(WriteLine), new Type[] { }); |
| 261 | + if (methodInfoLine == null) |
| 262 | + methodInfoLine = typeof(WriteConsole).GetMethod(nameof(WriteLine), new Type[] { }); |
| 263 | +#else |
| 264 | + MethodInfo methodInfoLine = currentContext.RealType.GetMethod(nameof(WriteLine), Array.Empty<Type>()); |
| 265 | + if (methodInfoLine == null) |
| 266 | + methodInfoLine = typeof(WriteConsole).GetMethod(nameof(WriteLine), Array.Empty<Type>()); |
| 267 | +#endif |
| 268 | + m_WriteLine = (Action)Delegate.CreateDelegate(typeof(Action), methodInfoLine); |
| 269 | + } |
| 270 | + |
| 271 | + private static Action<T> GetDelegate<T>(Type type, string methodName) |
| 272 | + { |
| 273 | + MethodInfo methodInfo = type.GetMethod(methodName, new Type[] { typeof(T) }); |
| 274 | + if (methodInfo == null) |
| 275 | + methodInfo = typeof(WriteConsole).GetMethod(methodName, new Type[] { typeof(T) }); |
| 276 | + |
| 277 | + return (Action<T>)Delegate.CreateDelegate(typeof(Action<T>), methodInfo); |
| 278 | + } |
| 279 | + |
| 280 | + public void Write(ulong value) { m_WriteUlong(value); } |
| 281 | + |
| 282 | + public void Write(uint value) { m_WriteUint(value); } |
| 283 | + |
| 284 | + public void Write(string value) { m_WriteString(value); } |
| 285 | + |
| 286 | + public void Write(float value) { m_WriteFloat(value); } |
| 287 | + |
| 288 | + public void Write(object value) { m_WriteObject(value); } |
| 289 | + |
| 290 | + public void Write(decimal value) { m_WriteDecimal(value); } |
| 291 | + |
| 292 | + public void Write(long value) { m_WriteLong(value); } |
| 293 | + |
| 294 | + public void Write(int value) { m_WriteInt(value); } |
| 295 | + |
| 296 | + public void Write(double value) { m_WriteDouble(value); } |
| 297 | + |
| 298 | + public void Write(char[] value) { m_WriteCharArray(value); } |
| 299 | + |
| 300 | + public void Write(char value) { m_WriteChar(value); } |
| 301 | + |
| 302 | + public void Write(bool value) { m_WriteBool(value); } |
| 303 | + |
| 304 | + public void Write(string format, object arg1) { m_WriteFormat1(format, arg1); } |
| 305 | + |
| 306 | + public void Write(string format, object arg1, object arg2) { m_WriteFormat2(format, arg1, arg2); } |
| 307 | + |
| 308 | + public void Write(string format, object arg1, object arg2, object arg3) { m_WriteFormat3(format, arg1, arg2, arg3); } |
| 309 | + |
| 310 | + public void Write(string format, params object[] args) { m_WriteFormatS(format, args); } |
| 311 | + |
| 312 | + public void WriteLine() { m_WriteLine(); } |
| 313 | + |
| 314 | + public void WriteLine(ulong value) { m_WriteLineUlong(value); } |
| 315 | + |
| 316 | + public void WriteLine(uint value) { m_WriteLineUint(value); } |
| 317 | + |
| 318 | + public void WriteLine(string value) { m_WriteLineString(value); } |
| 319 | + |
| 320 | + public void WriteLine(float value) { m_WriteLineFloat(value); } |
| 321 | + |
| 322 | + public void WriteLine(object value) { m_WriteLineObject(value); } |
| 323 | + |
| 324 | + public void WriteLine(decimal value) { m_WriteLineDecimal(value); } |
| 325 | + |
| 326 | + public void WriteLine(long value) { m_WriteLineLong(value); } |
| 327 | + |
| 328 | + public void WriteLine(int value) { m_WriteLineInt(value); } |
| 329 | + |
| 330 | + public void WriteLine(double value) { m_WriteLineDouble(value); } |
| 331 | + |
| 332 | + public void WriteLine(char[] value) { m_WriteLineCharArray(value); } |
| 333 | + |
| 334 | + public void WriteLine(char value) { m_WriteLineChar(value); } |
| 335 | + |
| 336 | + public void WriteLine(bool value) { m_WriteLineBool(value); } |
| 337 | + |
| 338 | + public void WriteLine(string format, object arg1) { m_WriteLineFormat1(format, arg1); } |
| 339 | + |
| 340 | + public void WriteLine(string format, object arg1, object arg2) { m_WriteLineFormat2(format, arg1, arg2); } |
| 341 | + |
| 342 | + public void WriteLine(string format, object arg1, object arg2, object arg3) { m_WriteLineFormat3(format, arg1, arg2, arg3); } |
| 343 | + |
| 344 | + public void WriteLine(string format, params object[] args) { m_WriteLineFormatS(format, args); } |
| 345 | + #endregion |
134 | 346 | }
|
135 | 347 | }
|
0 commit comments