8000 Extract utility method in TestDomainReload · pythonnet/pythonnet@d1799aa · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit d1799aa

Browse files
committed
Extract utility method in TestDomainReload
Addresses comment: #958
1 parent f575bd3 commit d1799aa

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/embed_tests/TestDomainReload.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,15 @@ public object Call(string methodName, params object[] args)
400400
return method.Invoke(null, args);
401401
}
402402
}
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+
}
403412

404413
/// <summary>
405414
/// Create a domain, run the assembly in it (the RunPython function),
@@ -412,10 +421,7 @@ static void RunAssemblyAndUnload(string domainName)
412421
AppDomain domain = CreateDomain(domainName);
413422
// Create a Proxy object in the new domain, where we want the
414423
// 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);
419425

420426
theProxy.Call("InitPython", ShutdownMode.Soft);
421427
// 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
484490
AppDomain domain = CreateDomain("test_domain_reload_1");
485491
try
486492
{
487-
var theProxy = (Proxy)domain.CreateInstanceAndUnwrap(
488-
type.Assembly.FullName,
489-
type.FullName);
493+
var theProxy = CreateInstanceInstanceAndUnwrap<Proxy>(domain);
490494
theProxy.Call("InitPython", ShutdownMode.Reload);
491495

492-
var caller = (T1)domain.CreateInstanceAndUnwrap(
493-
typeof(T1).Assembly.FullName,
494-
typeof(T1).FullName);
496+
var caller = CreateInstanceInstanceAndUnwrap<T1>(domain);
495497
arg = caller.Execute(arg);
496498

497499
theProxy.Call("ShutdownPython");
@@ -506,14 +508,10 @@ static void RunDomainReloadSteps<T1, T2>() where T1 : CrossCaller where T2 : Cro
506508
AppDomain domain = CreateDomain("test_domain_reload_2");
507509
try
508510
{
509-
var theProxy = (Proxy)domain.CreateInstanceAndUnwrap(
510-
type.Assembly.FullName,
511-
type.FullName);
511+
var theProxy = CreateInstanceInstanceAndUnwrap<Proxy>(domain);
512512
theProxy.Call("InitPython", ShutdownMode.Reload);
513513

514-
var caller = (T2)domain.CreateInstanceAndUnwrap(
515-
typeof(T2).Assembly.FullName,
516-
typeof(T2).FullName);
514+
var caller = CreateInstanceInstanceAndUnwrap<T2>(domain);
517515
caller.Execute(arg);
518516
theProxy.Call("ShutdownPythonCompletely");
519517
}

0 commit comments

Comments
 (0)
0