forked from dmitriyse/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonInterop.cs
More file actions
68 lines (59 loc) · 1.72 KB
/
PythonInterop.cs
File metadata and controls
68 lines (59 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
namespace Python.Runtime.Interop
{
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using InteropContracts;
public class PythonInterop : IPythonInterop
{
public IPythonNativeMethodsInterop NativeMethods { get; } = new PythonNativeMethodsInterop();
public IPythonRuntimeInterop Runtime { get; } = new PythonRuntimeInterop();
public OSPlatform TargetPlatform
{
get
{
#if MONO_LINUX
return OSPlatform.Linux;
#elif MONO_OSX
return OSPlatform.OSX;
#else
return OSPlatform.Windows;
#endif
}
}
public bool IsPyDebug
{
get
{
#if (PYTHON_WITH_PYDEBUG)
return true;
#else
return false;
#endif
}
}
public void InitExceptionOffset()
{
CopyStaticFields<ExceptionOffset, Python.Runtime.ExceptionOffset>();
}
public void InitTypeOffset()
{
CopyStaticFields<TypeOffset, Python.Runtime.TypeOffset>();
}
private void CopyStaticFields<TFrom, TTo>()
{
var type = typeof(TFrom);
var actualValues = type.GetFields().ToDictionary(x => x.Name);
var targetType = typeof(TTo);
var targetFields = targetType.GetFields();
for (var i = 0; i < targetFields.Length; i++)
{
FieldInfo actualField;
if (actualValues.TryGetValue(targetFields[i].Name, out actualField))
{
targetFields[i].SetValue(null, actualField.GetValue(null));
}
}
}
}
}