@@ -400,6 +400,15 @@ public object Call(string methodName, params object[] args)
400
400
return method . Invoke ( null , args ) ;
401
401
}
402
402
}
403
+
404
+ static T CreateInstanceInstanceAndUnwrap < T > ( AppDomain domain )
405
+ {
406
+ Type type = typeof ( T ) ;
407
+ var theProxy = ( T ) domain . CreateInstanceAndUnwrap (
408
+ type . Assembly . FullName ,
409
+ type . FullName ) ;
410
+ return theProxy ;
411
+ }
403
412
404
413
/// <summary>
405
414
/// Create a domain, run the assembly in it (the RunPython function),
@@ -412,10 +421,7 @@ static void RunAssemblyAndUnload(string domainName)
412
421
AppDomain domain = CreateDomain ( domainName ) ;
413
422
// Create a Proxy object in the new domain, where we want the
414
423
// assembly (and Python .NET) to reside
415
- Type type = typeof ( Proxy ) ;
416
- var theProxy = ( Proxy ) domain . CreateInstanceAndUnwrap (
417
- type . Assembly . FullName ,
418
- type . FullName ) ;
424
+ var theProxy = CreateInstanceInstanceAndUnwrap < Proxy > ( domain ) ;
419
425
420
426
theProxy . Call ( "InitPython" , ShutdownMode . Soft ) ;
421
427
// From now on use the Proxy to call into the new assembly
@@ -484,14 +490,10 @@ static void RunDomainReloadSteps<T1, T2>() where T1 : CrossCaller where T2 : Cro
484
490
AppDomain domain = CreateDomain ( "test_domain_reload_1" ) ;
485
491
try
486
492
{
487
- var theProxy = ( Proxy ) domain . CreateInstanceAndUnwrap (
488
- type . Assembly . FullName ,
489
- type . FullName ) ;
493
+ var theProxy = CreateInstanceInstanceAndUnwrap < Proxy > ( domain ) ;
490
494
theProxy . Call ( "InitPython" , ShutdownMode . Reload ) ;
491
495
492
- var caller = ( T1 ) domain . CreateInstanceAndUnwrap (
493
- typeof ( T1 ) . Assembly . FullName ,
494
- typeof ( T1 ) . FullName ) ;
496
+ var caller = CreateInstanceInstanceAndUnwrap < T1 > ( domain ) ;
495
497
arg = caller . Execute ( arg ) ;
496
498
497
499
theProxy . Call ( "ShutdownPython" ) ;
@@ -506,14 +508,10 @@ static void RunDomainReloadSteps<T1, T2>() where T1 : CrossCaller where T2 : Cro
506
508
AppDomain domain = CreateDomain ( "test_domain_reload_2" ) ;
507
509
try
508
510
{
509
- var theProxy = ( Proxy ) domain . CreateInstanceAndUnwrap (
510
- type . Assembly . FullName ,
511
- type . FullName ) ;
511
+ var theProxy = CreateInstanceInstanceAndUnwrap < Proxy > ( domain ) ;
512
512
theProxy . Call ( "InitPython" , ShutdownMode . Reload ) ;
513
513
514
- var caller = ( T2 ) domain . CreateInstanceAndUnwrap (
515
- typeof ( T2 ) . Assembly . FullName ,
516
- typeof ( T2 ) . FullName ) ;
514
+ var caller = CreateInstanceInstanceAndUnwrap < T2 > ( domain ) ;
517
515
caller . Execute ( arg ) ;
518
516
theProxy . Call ( "ShutdownPythonCompletely" ) ;
519
517
}
0 commit comments