开发者

Issue with using a .net class in ironpython

开发者 https://www.devze.com 2022-12-08 13:30 出处:网络
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

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 tri开发者_StackOverflow社区ed 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


You have to use a bare import like so:

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

精彩评论

暂无评论...
验证码 换一张
取 消