Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

69

70

71

72

73

74

75

76

77

78

79

80

""" 

@file 

@brief Add references to DLL. 

""" 

from clr import AddReference as ClrAddReference 

 

 

def AddReference(name, version=None): 

""" 

Imports a :epkg:`C#` dll. 

 

@param name name of the DLL. 

@param version `'Release'` or `'Debug'` 

@return imported DLL 

 

If the version is None, it first tries `'Release'` 

then `'Debug'`. 

""" 

try: 

return ClrAddReference(name) 

except Exception as e: 

if "Unable to find assembly" in str(e): 

import sys 

import os 

this = os.path.abspath(os.path.dirname(__file__)) 

if version is None: 

vers = 'Release' 

else: 

vers = version 

rel = os.path.join(this, vers) 

if os.path.exists(os.path.join(rel, '__init__.py')): 

this = rel 

elif version is None: 

rel = os.path.join(this, "Debug") 

if not os.path.exists(os.path.join(rel, '__init__.py')): 

raise FileNotFoundError( 

"Unable to find folders 'Release' or 'Debug' in '{0}'".format(this)) 

this = rel 

if this and os.path.exists(this): 

sys.path.append(this) 

try: 

res = ClrAddReference(name) 

except Exception: 

del sys.path[-1] 

raise 

del sys.path[-1] 

return res 

else: 

raise 

else: 

raise 

 

 

def add_csharpml_extension(): 

""" 

Imports *CSharpExtension* into global context. 

 

This binary has a version. On :epkg:`Windows`, 

the system might decide to skip the replacement 

of an assembly because it is in use. You can 

check the version of this by using the following code. 

 

.. exref:: 

:title: Imports the C# extension into Python 

 

.. runpython:: 

:showcode: 

 

from csharpyml.binaries import add_csharpml_extension 

from csharpyml import __version__ 

 

add_csharpml_extension() 

 

# This line needs to be after the previous one. 

from CSharPyMLExtension import Constants 

 

vers = Constants.Version() 

print(__version__, vers) 

""" 

AddReference("CSharPyMLExtension")