Issue with using a .net class in ironpython

元气小坏坏 提交于 2019-12-01 22:31:14

问题


If I have a .Net class that is not part of any namespace then I'm not able to use it in ironpython.

Here is an example

Suppose I have a assembly FooLib.dll with the following class definition

//note the following class is not part of global namespace

public class Foo { }

Now I try to use it in ironpython

clr.AddReference("FooLib") # This call succeeds.

f = Foo()

The line f= Foo() returns the error

Traceback (most recent call last):

File "", line 1, in

NameError: name 'Foo' is not defined

I tried the following

from FooLib import *

f = Foo()

The line from FooLib import * reports an error which does make sense as the from clause should be used on namespaces and not assemblies

However, If the class Foo belong to some namespace, then i don't have a problem importing in ironpython

So, my query is how do I use a .net class belonging to a global namespace from ironpython

regards Ganesh


回答1:


You have to use a bare import like so:

import clr
clr.AddReference("FooLib") # This call succeeds.
import Foo
f = Foo()


来源:https://stackoverflow.com/questions/1579922/issue-with-using-a-net-class-in-ironpython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!