[Python.NET] How to create an instance of C# from pythonnet?
Stephen P. Lepisto
stephenp at otakuworld.com
Fri Apr 19 06:14:44 CEST 2013
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
On 4/18/2013 3:42 PM, Seungweon Park wrote:
> 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?
>
>
>
> On Thu, Apr 18, 2013 at 3:07 PM, msutton <msutton at ucsd.edu
> <mailto:msutton at ucsd.edu>> wrote:
>
> 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
>
>
> On 04/18/2013 02:45 PM, Seungweon Park wrote:
>> 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
>>
>>
>> _________________________________________________
>> Python.NET mailing list -PythonDotNet at python.org <mailto:PythonDotNet at python.org>
>> http://mail.python.org/mailman/listinfo/pythondotnet
>
>
>
>
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet
--
Stephen P. Lepisto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20130418/b32856ac/attachment-0001.html>
More information about the PythonDotNet
mailing list