Discussion:
[Python.NET] How to create an instance of C# from pythonnet?
Seungweon Park
2013-04-18 21:45:27 UTC
Permalink
Hi,

I have Adapter.dll with namespace 'AAA.BBB.Adapter" written in C# which
gives network adapter information.
I want to call one of method GetSpeed(). I don't know How to create an
instance in python.
Would you give me some clue for writing the same python code like below
powershell script using python.net
I don't see any sample code which python creates C# class instance.


Python
---------------------------
C:\CVS\Pythonnet>npython
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
import os,sys
sys.path.append("c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions")
sys.path
['', 'C:\\Python27\\lib\\site-packages\\paramiko-1.10.0-py2.7.egg',
'C:\\CVS\\Python\\libs', 'C:\\Windows\\system32\\pyt
hon27.zip', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk', 'C:\\CVS\\Pythonnet', 'C:\\Python27'
, 'C:\\Python27\\lib\\site-packages',
'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\',
'c:\\cvs\\powershelliteexam
ple\\networktestlibrary\\iteextensions']
import clr
clr.FindAssembly("Adapter")
u'c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions\\Adapter.dll'
clr.AddReference("Adapter")
<System.Reflection.RuntimeAssembly object at 0x052028A0>

I don't know how to create an instance of Adapter.


Thank you,
Spark.


Powershell
----------------------------
PS C:\CVS\PowershellITEExample> Import-Module .\LoadAdapter.psd1
PS C:\CVS\PowershellITEExample> $adapter = New-object -type
"AAA.BBB.Adapter"
PS C:\CVS\PowershellITEExample> $ret = $adapter.GetAdapter("TestAdapter",
"oids.xml")
PS C:\CVS\PowershellITEExample> $ret.Passed
True
PS C:\CVS\PowershellITEExample> $baseAdapter = $ret.FunctionReturnValue
PS C:\CVS\PowershellITEExample> $baseAdapter.GetSpeed()

Passed : True
Failed : False
Errored : False
Status : Pass
Description : Speed retrieved successfully.
FunctionReturnValue : auto
msutton
2013-04-18 22:07:53 UTC
Permalink
What I use is:

import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName('Demo')
from Render import Demo

where the C# code has namespace Render and public class Demo.

-Manuel
Post by Seungweon Park
Hi,
I have Adapter.dll with namespace 'AAA.BBB.Adapter" written in C#
which gives network adapter information.
I want to call one of method GetSpeed(). I don't know How to create an
instance in python.
Would you give me some clue for writing the same python code like
below powershell script using python.net <http://python.net>
I don't see any sample code which python creates C# class instance.
Python
---------------------------
C:\CVS\Pythonnet>npython
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import os,sys
sys.path.append("c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions")
sys.path
['', 'C:\\Python27\\lib\\site-packages\\paramiko-1.10.0-py2.7.egg',
'C:\\CVS\\Python\\libs', 'C:\\Windows\\system32\\pyt
hon27.zip', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk', 'C:\\CVS\\Pythonnet', 'C:\\Python27'
, 'C:\\Python27\\lib\\site-packages',
'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\',
'c:\\cvs\\powershelliteexam
ple\\networktestlibrary\\iteextensions']
import clr
clr.FindAssembly("Adapter")
u'c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions\\Adapter.dll'
clr.AddReference("Adapter")
<System.Reflection.RuntimeAssembly object at 0x052028A0>
I don't know how to create an instance of Adapter.
Thank you,
Spark.
Powershell
----------------------------
PS C:\CVS\PowershellITEExample> Import-Module .\LoadAdapter.psd1
PS C:\CVS\PowershellITEExample> $adapter = New-object -type
"AAA.BBB.Adapter"
PS C:\CVS\PowershellITEExample> $ret =
$adapter.GetAdapter("TestAdapter", "oids.xml")
PS C:\CVS\PowershellITEExample> $ret.Passed
True
PS C:\CVS\PowershellITEExample> $baseAdapter = $ret.FunctionReturnValue
PS C:\CVS\PowershellITEExample> $baseAdapter.GetSpeed()
Passed : True
Failed : False
Errored : False
Status : Pass
Description : Speed retrieved successfully.
FunctionReturnValue : auto
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Seungweon Park
2013-04-18 22:42:46 UTC
Permalink
Thank you for the reply.

I did
import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName("Adapter")
<System.Reflection.RuntimeAssembly object at 0x05381148>
from AAA.BBB import Adapter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named AAA.BBB

I don't know why I got this. :-(
Any idea?
import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName('Demo')
from Render import Demo
where the C# code has namespace Render and public class Demo.
-Manuel
Hi,
I have Adapter.dll with namespace 'AAA.BBB.Adapter" written in C# which
gives network adapter information.
I want to call one of method GetSpeed(). I don't know How to create an
instance in python.
Would you give me some clue for writing the same python code like below
powershell script using python.net
I don't see any sample code which python creates C# class instance.
Python
---------------------------
C:\CVS\Pythonnet>npython
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
import os,sys
sys.path.append("c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions")
sys.path
['', 'C:\\Python27\\lib\\site-packages\\paramiko-1.10.0-py2.7.egg',
'C:\\CVS\\Python\\libs', 'C:\\Windows\\system32\\pyt
hon27.zip', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk', 'C:\\CVS\\Pythonnet', 'C:\\Python27'
, 'C:\\Python27\\lib\\site-packages',
'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\',
'c:\\cvs\\powershelliteexam
ple\\networktestlibrary\\iteextensions']
import clr
clr.FindAssembly("Adapter")
u'c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions\\Adapter.dll'
clr.AddReference("Adapter")
<System.Reflection.RuntimeAssembly object at 0x052028A0>
I don't know how to create an instance of Adapter.
Thank you,
Spark.
Powershell
----------------------------
PS C:\CVS\PowershellITEExample> Import-Module .\LoadAdapter.psd1
PS C:\CVS\PowershellITEExample> $adapter = New-object -type
"AAA.BBB.Adapter"
PS C:\CVS\PowershellITEExample> $ret = $adapter.GetAdapter("TestAdapter",
"oids.xml")
PS C:\CVS\PowershellITEExample> $ret.Passed
True
PS C:\CVS\PowershellITEExample> $baseAdapter = $ret.FunctionReturnValue
PS C:\CVS\PowershellITEExample> $baseAdapter.GetSpeed()
Passed : True
Failed : False
Errored : False
Status : Pass
Description : Speed retrieved successfully.
FunctionReturnValue : auto
_________________________________________________
msutton
2013-04-18 23:01:52 UTC
Permalink
How about skip the import giving the error and call:
clr.AAA.BBB.Adapter()

I really don't know much about C#, or nested namespaces being called
from python.net. You could try making a simpler C# dll and getting that
to work and isolate the issue, which I think is the dot notated nested
namespaces.

-Manuel
Post by Seungweon Park
Thank you for the reply.
I did
Post by Seungweon Park
import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName("Adapter")
<System.Reflection.RuntimeAssembly object at 0x05381148>
Post by Seungweon Park
from AAA.BBB import Adapter
File "<stdin>", line 1, in <module>
ImportError: No module named AAA.BBB
I don't know why I got this. :-(
Any idea?
import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName('Demo')
from Render import Demo
where the C# code has namespace Render and public class Demo.
-Manuel
Post by Seungweon Park
Hi,
I have Adapter.dll with namespace 'AAA.BBB.Adapter" written in C#
which gives network adapter information.
I want to call one of method GetSpeed(). I don't know How to
create an instance in python.
Would you give me some clue for writing the same python code like
below powershell script using python.net <http://python.net>
I don't see any sample code which python creates C# class instance.
Python
---------------------------
C:\CVS\Pythonnet>npython
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more
information.
import os,sys
sys.path.append("c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions")
sys.path
['',
'C:\\Python27\\lib\\site-packages\\paramiko-1.10.0-py2.7.egg',
'C:\\CVS\\Python\\libs', 'C:\\Windows\\system32\\pyt
hon27.zip', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk', 'C:\\CVS\\Pythonnet', 'C:\\Python27'
, 'C:\\Python27\\lib\\site-packages',
'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\',
'c:\\cvs\\powershelliteexam
ple\\networktestlibrary\\iteextensions']
import clr
clr.FindAssembly("Adapter")
u'c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions\\Adapter.dll'
clr.AddReference("Adapter")
<System.Reflection.RuntimeAssembly object at 0x052028A0>
I don't know how to create an instance of Adapter.
Thank you,
Spark.
Powershell
----------------------------
PS C:\CVS\PowershellITEExample> Import-Module .\LoadAdapter.psd1
PS C:\CVS\PowershellITEExample> $adapter = New-object -type
"AAA.BBB.Adapter"
PS C:\CVS\PowershellITEExample> $ret =
$adapter.GetAdapter("TestAdapter", "oids.xml")
PS C:\CVS\PowershellITEExample> $ret.Passed
True
PS C:\CVS\PowershellITEExample> $baseAdapter =
$ret.FunctionReturnValue
PS C:\CVS\PowershellITEExample> $baseAdapter.GetSpeed()
Passed : True
Failed : False
Errored : False
Status : Pass
Description : Speed retrieved successfully.
FunctionReturnValue : auto
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Stephen P. Lepisto
2013-04-19 04:14:44 UTC
Permalink
Try this from python with pythonNet installed (the current directory is
assumed to contain Adapter.dll):

import clr
clr.AddReference("Adapter") # Load the Adapter.dll
from AAA.BBB import Adapter # Get the Adapter class type
adapter = Adapter() # Create instance of the Adapter class
adapter.GetSpeed() # Call the method
Post by Seungweon Park
Thank you for the reply.
I did
Post by Seungweon Park
import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName("Adapter")
<System.Reflection.RuntimeAssembly object at 0x05381148>
Post by Seungweon Park
from AAA.BBB import Adapter
File "<stdin>", line 1, in <module>
ImportError: No module named AAA.BBB
I don't know why I got this. :-(
Any idea?
import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName('Demo')
from Render import Demo
where the C# code has namespace Render and public class Demo.
-Manuel
Post by Seungweon Park
Hi,
I have Adapter.dll with namespace 'AAA.BBB.Adapter" written in C#
which gives network adapter information.
I want to call one of method GetSpeed(). I don't know How to
create an instance in python.
Would you give me some clue for writing the same python code like
below powershell script using python.net <http://python.net>
I don't see any sample code which python creates C# class instance.
Python
---------------------------
C:\CVS\Pythonnet>npython
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more
information.
import os,sys
sys.path.append("c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions")
sys.path
['',
'C:\\Python27\\lib\\site-packages\\paramiko-1.10.0-py2.7.egg',
'C:\\CVS\\Python\\libs', 'C:\\Windows\\system32\\pyt
hon27.zip', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk', 'C:\\CVS\\Pythonnet', 'C:\\Python27'
, 'C:\\Python27\\lib\\site-packages',
'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\',
'c:\\cvs\\powershelliteexam
ple\\networktestlibrary\\iteextensions']
import clr
clr.FindAssembly("Adapter")
u'c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions\\Adapter.dll'
clr.AddReference("Adapter")
<System.Reflection.RuntimeAssembly object at 0x052028A0>
I don't know how to create an instance of Adapter.
Thank you,
Spark.
Powershell
----------------------------
PS C:\CVS\PowershellITEExample> Import-Module .\LoadAdapter.psd1
PS C:\CVS\PowershellITEExample> $adapter = New-object -type
"AAA.BBB.Adapter"
PS C:\CVS\PowershellITEExample> $ret =
$adapter.GetAdapter("TestAdapter", "oids.xml")
PS C:\CVS\PowershellITEExample> $ret.Passed
True
PS C:\CVS\PowershellITEExample> $baseAdapter =
$ret.FunctionReturnValue
PS C:\CVS\PowershellITEExample> $baseAdapter.GetSpeed()
Passed : True
Failed : False
Errored : False
Status : Pass
Description : Speed retrieved successfully.
FunctionReturnValue : auto
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
--
Stephen P. Lepisto
Seungweon Park
2013-04-19 17:45:34 UTC
Permalink
Thank you for the replies.

Now, I recompiled Python.Net with .Net 4.0 and get below working with
simple AAA.BBB.Adapter after reading

http://stackoverflow.com/questions/14520888/clr-addreferenceexample-file-unable-to-find-assembly?rq=1
http://stackoverflow.com/questions/13259617/python-for-net-unable-to-find-assembly-error?rq=1
http://sourceforge.net/p/pythonnet/bugs/17/
http://sourceforge.net/p/pythonnet/bugs/18/


Now I have below sample code(Adapter.proj has two c# files), and
experimented

Class1.cs
-----------
namespace AAA.BBB.CCC
{

public class Adapter
{
public string Function1()
{
return "AAA.BBB.CCC: Adapter Function1 Called";
}
public void Function2()
{
Console.WriteLine("AAA.BBB.CCC: Adapter Function2");
}
}

public class Network
{
public string Function1()
{
return "AAA.BBB.CCC: Network Function1 called";
}

public void Function2()
{
Console.WriteLine("AAA.BBB.CCC: Network Function2");
}
}
}

namespace AAA.BBB
{
public class Adapter
{
public string Function1()
{
return "AAA.BBB: Adapter Function1 Called";
}
public void Function2()
{
Console.WriteLine("AAA.BBB: Adapter Function2");
}
}

public class Network
{
public string Function1()
{
return "AAA.BBB: Network Function1 called";
}

public void Function2()
{
Console.WriteLine("AAA.BBB: Network Function2");
}
}

}

Class2.cs
-----------
namespace AAA.CCC
{
public class Adapter
{
public string Function1()
{
return "AAA.CCC: Adapter Function1 Called";
}
public void Function2()
{
Console.WriteLine("AAA.CCC: Adapter Function2");
}
}
}

Then, I got below. It seems working great.

C:\Automation\PythonNet>nPython.exe
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
Post by Stephen P. Lepisto
import sys
sys.path.append("c:\\automation\\pythonnet")
import clr
clr.FindAssembly("Adapter")
u'Adapter.dll'
Post by Stephen P. Lepisto
clr.AddReference("Adapter")
<System.Reflection.RuntimeAssembly object at 0x052E5828>
Post by Stephen P. Lepisto
adapter1 = Adapter()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Adapter' is not defined
Post by Stephen P. Lepisto
from AAA.BBB.CCC import Adapter
from AAA.BBB.CCC import *
adapter1 = Adapter()
adapter1.Function1()
u'AAA.BBB.CCC: Adapter Function1 Called'
Post by Stephen P. Lepisto
adapter1.Function2()
AAA.BBB.CCC: Adapter Function2
Post by Stephen P. Lepisto
network1 = Network()
network1.Function1()
u'AAA.BBB.CCC: Network Function1 called'
Post by Stephen P. Lepisto
from AAA.CCC import *
adapter1.Function1()
u'AAA.BBB.CCC: Adapter Function1 Called'
Post by Stephen P. Lepisto
adapter1 = Adapter()
adapter1.Function1()
u'AAA.CCC: Adapter Function1 Called'
My questions are

1. I don't know how to specify different namespace of class when creating
an instance.
2. The class that I already have has similar above layout in a project.
Common project has many class files in it, and each class has different
name space and class name.
AddReference('Common') looks working and loaded, but can't import.
Post by Stephen P. Lepisto
clr.AddReference('Common')
<System.Reflection.RuntimeAssembly object at 0x052E73C8>
Post by Stephen P. Lepisto
from Network import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Network
Post by Stephen P. Lepisto
from Network.Common import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Network.Common
Difference is that Common project has [Serializable] and enabled "Make
assembly COM-Visible"

[Serializable]
[Description("SimpleDictionary")]
public class SimpleDictionary : ISimpleDictionary
{
...
}

Any thoughts?
Wonder if there is a way to list available class after AddReference().

Thanks,
Spark.
Post by Stephen P. Lepisto
Try this from python with pythonNet installed (the current directory is
import clr
clr.AddReference("Adapter") # Load the Adapter.dll
from AAA.BBB import Adapter # Get the Adapter class type
adapter = Adapter() # Create instance of the Adapter class
adapter.GetSpeed() # Call the method
Thank you for the reply.
I did
import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName("Adapter")
<System.Reflection.RuntimeAssembly object at 0x05381148>
from AAA.BBB import Adapter
File "<stdin>", line 1, in <module>
ImportError: No module named AAA.BBB
I don't know why I got this. :-(
Any idea?
import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName('Demo')
from Render import Demo
where the C# code has namespace Render and public class Demo.
-Manuel
Hi,
I have Adapter.dll with namespace 'AAA.BBB.Adapter" written in C# which
gives network adapter information.
I want to call one of method GetSpeed(). I don't know How to create an
instance in python.
Would you give me some clue for writing the same python code like below
powershell script using python.net
I don't see any sample code which python creates C# class instance.
Python
---------------------------
C:\CVS\Pythonnet>npython
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
import os,sys
sys.path.append("c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions")
sys.path
['', 'C:\\Python27\\lib\\site-packages\\paramiko-1.10.0-py2.7.egg',
'C:\\CVS\\Python\\libs', 'C:\\Windows\\system32\\pyt
hon27.zip', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk', 'C:\\CVS\\Pythonnet', 'C:\\Python27'
, 'C:\\Python27\\lib\\site-packages',
'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\',
'c:\\cvs\\powershelliteexam
ple\\networktestlibrary\\iteextensions']
import clr
clr.FindAssembly("Adapter")
u'c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions\\Adapter.dll'
clr.AddReference("Adapter")
<System.Reflection.RuntimeAssembly object at 0x052028A0>
I don't know how to create an instance of Adapter.
Thank you,
Spark.
Powershell
----------------------------
PS C:\CVS\PowershellITEExample> Import-Module .\LoadAdapter.psd1
PS C:\CVS\PowershellITEExample> $adapter = New-object -type
"AAA.BBB.Adapter"
PS C:\CVS\PowershellITEExample> $ret = $adapter.GetAdapter("TestAdapter",
"oids.xml")
PS C:\CVS\PowershellITEExample> $ret.Passed
True
PS C:\CVS\PowershellITEExample> $baseAdapter = $ret.FunctionReturnValue
PS C:\CVS\PowershellITEExample> $baseAdapter.GetSpeed()
Passed : True
Failed : False
Errored : False
Status : Pass
Description : Speed retrieved successfully.
FunctionReturnValue : auto
_________________________________________________
_________________________________________________
--
Stephen P. Lepisto
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Seungweon Park
2013-04-19 22:47:52 UTC
Permalink
I think I resolved the issue which I need to import reference dlls prior to
import "from Network.Common import *".
Common uses many refereces from external *.dlls, once I imported all, then
I can import Common dll and use correctly.

Thank you for your helps.
Spark.
Post by Seungweon Park
Thank you for the replies.
Now, I recompiled Python.Net with .Net 4.0 and get below working with
simple AAA.BBB.Adapter after reading
http://stackoverflow.com/questions/14520888/clr-addreferenceexample-file-unable-to-find-assembly?rq=1
http://stackoverflow.com/questions/13259617/python-for-net-unable-to-find-assembly-error?rq=1
http://sourceforge.net/p/pythonnet/bugs/17/
http://sourceforge.net/p/pythonnet/bugs/18/
Now I have below sample code(Adapter.proj has two c# files), and
experimented
Class1.cs
-----------
namespace AAA.BBB.CCC
{
public class Adapter
{
public string Function1()
{
return "AAA.BBB.CCC: Adapter Function1 Called";
}
public void Function2()
{
Console.WriteLine("AAA.BBB.CCC: Adapter Function2");
}
}
public class Network
{
public string Function1()
{
return "AAA.BBB.CCC: Network Function1 called";
}
public void Function2()
{
Console.WriteLine("AAA.BBB.CCC: Network Function2");
}
}
}
namespace AAA.BBB
{
public class Adapter
{
public string Function1()
{
return "AAA.BBB: Adapter Function1 Called";
}
public void Function2()
{
Console.WriteLine("AAA.BBB: Adapter Function2");
}
}
public class Network
{
public string Function1()
{
return "AAA.BBB: Network Function1 called";
}
public void Function2()
{
Console.WriteLine("AAA.BBB: Network Function2");
}
}
}
Class2.cs
-----------
namespace AAA.CCC
{
public class Adapter
{
public string Function1()
{
return "AAA.CCC: Adapter Function1 Called";
}
public void Function2()
{
Console.WriteLine("AAA.CCC: Adapter Function2");
}
}
}
Then, I got below. It seems working great.
C:\Automation\PythonNet>nPython.exe
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
Post by Stephen P. Lepisto
import sys
sys.path.append("c:\\automation\\pythonnet")
import clr
clr.FindAssembly("Adapter")
u'Adapter.dll'
Post by Stephen P. Lepisto
clr.AddReference("Adapter")
<System.Reflection.RuntimeAssembly object at 0x052E5828>
Post by Stephen P. Lepisto
adapter1 = Adapter()
File "<stdin>", line 1, in <module>
NameError: name 'Adapter' is not defined
Post by Stephen P. Lepisto
from AAA.BBB.CCC import Adapter
from AAA.BBB.CCC import *
adapter1 = Adapter()
adapter1.Function1()
u'AAA.BBB.CCC: Adapter Function1 Called'
Post by Stephen P. Lepisto
adapter1.Function2()
AAA.BBB.CCC: Adapter Function2
Post by Stephen P. Lepisto
network1 = Network()
network1.Function1()
u'AAA.BBB.CCC: Network Function1 called'
Post by Stephen P. Lepisto
from AAA.CCC import *
adapter1.Function1()
u'AAA.BBB.CCC: Adapter Function1 Called'
Post by Stephen P. Lepisto
adapter1 = Adapter()
adapter1.Function1()
u'AAA.CCC: Adapter Function1 Called'
My questions are
1. I don't know how to specify different namespace of class when creating
an instance.
2. The class that I already have has similar above layout in a project.
Common project has many class files in it, and each class has different
name space and class name.
AddReference('Common') looks working and loaded, but can't import.
Post by Stephen P. Lepisto
clr.AddReference('Common')
<System.Reflection.RuntimeAssembly object at 0x052E73C8>
Post by Stephen P. Lepisto
from Network import *
File "<stdin>", line 1, in <module>
ImportError: No module named Network
Post by Stephen P. Lepisto
from Network.Common import *
File "<stdin>", line 1, in <module>
ImportError: No module named Network.Common
Difference is that Common project has [Serializable] and enabled "Make
assembly COM-Visible"
[Serializable]
[Description("SimpleDictionary")]
public class SimpleDictionary : ISimpleDictionary
{
...
}
Any thoughts?
Wonder if there is a way to list available class after AddReference().
Thanks,
Spark.
On Thu, Apr 18, 2013 at 9:14 PM, Stephen P. Lepisto <
Post by Stephen P. Lepisto
Try this from python with pythonNet installed (the current directory is
import clr
clr.AddReference("Adapter") # Load the Adapter.dll
from AAA.BBB import Adapter # Get the Adapter class type
adapter = Adapter() # Create instance of the Adapter class
adapter.GetSpeed() # Call the method
Thank you for the reply.
I did
import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName("Adapter")
<System.Reflection.RuntimeAssembly object at 0x05381148>
from AAA.BBB import Adapter
File "<stdin>", line 1, in <module>
ImportError: No module named AAA.BBB
I don't know why I got this. :-(
Any idea?
import clr
from System.Reflection import Assembly
Assembly.LoadWithPartialName('Demo')
from Render import Demo
where the C# code has namespace Render and public class Demo.
-Manuel
Hi,
I have Adapter.dll with namespace 'AAA.BBB.Adapter" written in C#
which gives network adapter information.
I want to call one of method GetSpeed(). I don't know How to create an
instance in python.
Would you give me some clue for writing the same python code like below
powershell script using python.net
I don't see any sample code which python creates C# class instance.
Python
---------------------------
C:\CVS\Pythonnet>npython
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import os,sys
sys.path.append("c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions")
sys.path
['', 'C:\\Python27\\lib\\site-packages\\paramiko-1.10.0-py2.7.egg',
'C:\\CVS\\Python\\libs', 'C:\\Windows\\system32\\pyt
hon27.zip', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk', 'C:\\CVS\\Pythonnet', 'C:\\Python27'
, 'C:\\Python27\\lib\\site-packages',
'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\',
'c:\\cvs\\powershelliteexam
ple\\networktestlibrary\\iteextensions']
import clr
clr.FindAssembly("Adapter")
u'c:\\cvs\\powershelliteexample\\networktestlibrary\\iteextensions\\Adapter.dll'
clr.AddReference("Adapter")
<System.Reflection.RuntimeAssembly object at 0x052028A0>
I don't know how to create an instance of Adapter.
Thank you,
Spark.
Powershell
----------------------------
PS C:\CVS\PowershellITEExample> Import-Module .\LoadAdapter.psd1
PS C:\CVS\PowershellITEExample> $adapter = New-object -type
"AAA.BBB.Adapter"
PS C:\CVS\PowershellITEExample> $ret =
$adapter.GetAdapter("TestAdapter", "oids.xml")
PS C:\CVS\PowershellITEExample> $ret.Passed
True
PS C:\CVS\PowershellITEExample> $baseAdapter = $ret.FunctionReturnValue
PS C:\CVS\PowershellITEExample> $baseAdapter.GetSpeed()
Passed : True
Failed : False
Errored : False
Status : Pass
Description : Speed retrieved successfully.
FunctionReturnValue : auto
_________________________________________________
_________________________________________________
--
Stephen P. Lepisto
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Loading...