|
1 | 1 | #if !NET452 |
2 | 2 | using System; |
3 | 3 | using System.IO; |
| 4 | +using System.Reflection; |
4 | 5 |
|
5 | 6 | using Xunit; |
6 | 7 |
|
@@ -80,6 +81,27 @@ string TestAllowReflectionSetting(bool allowReflection) |
80 | 81 | Assert.Equal("Property 'GetType' of object is not a function", exception.Description); |
81 | 82 | } |
82 | 83 |
|
| 84 | + [Fact] |
| 85 | + public override void EmbeddingOfInstanceOfAssemblyTypeAndCallingOfItsCreateInstanceMethod() |
| 86 | + { |
| 87 | + // Arrange |
| 88 | + string TestAllowReflectionSetting(bool allowReflection) |
| 89 | + { |
| 90 | + Assembly assembly = this.GetType().Assembly; |
| 91 | + string personTypeName = typeof(Person).FullName; |
| 92 | + |
| 93 | + using (var jsEngine = CreateJsEngine(allowReflection: allowReflection)) |
| 94 | + { |
| 95 | + jsEngine.EmbedHostObject("assembly", assembly); |
| 96 | + return jsEngine.Evaluate<string>("assembly.CreateInstance(\"" + personTypeName + "\");"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Act and Assert |
| 101 | + Assert.Equal("{FirstName=,LastName=}", TestAllowReflectionSetting(true)); |
| 102 | + Assert.Equal("{FirstName=,LastName=}", TestAllowReflectionSetting(false)); |
| 103 | + } |
| 104 | + |
83 | 105 | #endregion |
84 | 106 |
|
85 | 107 | #region Delegates |
@@ -432,6 +454,59 @@ string TestAllowReflectionSetting(bool allowReflection) |
432 | 454 |
|
433 | 455 | #endregion |
434 | 456 |
|
| 457 | + #region Types with methods |
| 458 | + |
| 459 | + [Fact] |
| 460 | + public override void EmbeddingOfTypeAndCallingOfItsGetTypeMethod() |
| 461 | + { |
| 462 | + // Arrange |
| 463 | + string dateTimeTypeName = typeof(DateTime).FullName; |
| 464 | + |
| 465 | + string TestAllowReflectionSetting(bool allowReflection) |
| 466 | + { |
| 467 | + Type type = typeof(Type); |
| 468 | + |
| 469 | + using (var jsEngine = CreateJsEngine(allowReflection: allowReflection)) |
| 470 | + { |
| 471 | + jsEngine.EmbedHostType("Type", type); |
| 472 | + return jsEngine.Evaluate<string>("Type.GetType(\"" + dateTimeTypeName + "\");"); |
| 473 | + } |
| 474 | + } |
| 475 | + |
| 476 | + // Act and Assert |
| 477 | + var exception1 = Assert.Throws<JsRuntimeException>(() => TestAllowReflectionSetting(false)); |
| 478 | + Assert.Equal("Runtime error", exception1.Category); |
| 479 | + Assert.Equal("Property 'GetType' of object is not a function", exception1.Description); |
| 480 | + |
| 481 | + var exception2 = Assert.Throws<JsRuntimeException>(() => TestAllowReflectionSetting(false)); |
| 482 | + Assert.Equal("Runtime error", exception2.Category); |
| 483 | + Assert.Equal("Property 'GetType' of object is not a function", exception2.Description); |
| 484 | + } |
| 485 | + |
| 486 | + [Fact] |
| 487 | + public override void EmbeddingOfAssemblyTypeAndCallingOfItsLoadMethod() |
| 488 | + { |
| 489 | + // Arrange |
| 490 | + const string reflectionEmitAssemblyName = "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; |
| 491 | + |
| 492 | + string TestAllowReflectionSetting(bool allowReflection) |
| 493 | + { |
| 494 | + Type assemblyType = typeof(Assembly); |
| 495 | + |
| 496 | + using (var jsEngine = CreateJsEngine(allowReflection: allowReflection)) |
| 497 | + { |
| 498 | + jsEngine.EmbedHostType("Assembly", assemblyType); |
| 499 | + return jsEngine.Evaluate<string>("Assembly.Load(\"" + reflectionEmitAssemblyName + "\");"); |
| 500 | + } |
| 501 | + } |
| 502 | + |
| 503 | + // Act and Assert |
| 504 | + Assert.Equal(reflectionEmitAssemblyName, TestAllowReflectionSetting(true)); |
| 505 | + Assert.Equal(reflectionEmitAssemblyName, TestAllowReflectionSetting(false)); |
| 506 | + } |
| 507 | + |
| 508 | + #endregion |
| 509 | + |
435 | 510 | #endregion |
436 | 511 | } |
437 | 512 | } |
|
0 commit comments