在IronPython中使用.net类的问题 [英] Issue with using a .net class in ironpython

查看:55
本文介绍了在IronPython中使用.net类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的.Net类不属于任何命名空间,那么我将无法在ironpython中使用它.

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

这是一个例子

假设我具有以下类定义的程序集FooLib.dll

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

//请注意以下类不是全局名称空间的一部分

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

公共类Foo {}

public class Foo { }

现在我尝试在ironpython中使用它

Now I try to use it in ironpython

clr.AddReference("FooLib")#此调用 成功.

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

f = Foo()

f = Foo()行返回错误

回溯(最近通话最近一次):

Traceback (most recent call last):

文件",位于

NameError:名称"Foo"未定义

NameError: name 'Foo' is not defined

我尝试了以下

从FooLib导入*

from FooLib import *

f = Foo()

来自FooLib import * 的行报告了一个错误,这确实是有道理的,因为from子句应该用于命名空间而不是程序集上

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

但是,如果Foo类属于某个命名空间,那么在ironpython中导入我没有问题

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

所以,我的查询是如何使用来自Ironpython的全局名称空间的.net类

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

致谢 加内什

推荐答案

您必须像这样使用裸露的import:

You have to use a bare import like so:

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

这篇关于在IronPython中使用.net类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆