简体   繁体   English

是否可以从IronPython调用重载的C#构造函数?

[英]Is it possible to call an overloaded C# constructor from IronPython?

I'm trying to create a SessionState object using a TextReader. 我正在尝试使用TextReader创建SessionState对象。 https://github.com/connamara/quickfixn/blob/master/QuickFIXn/SessionSettings.cs https://github.com/connamara/quickfixn/blob/master/QuickFIXn/SessionSettings.cs

public SessionSettings(string file){
        }
public SessionSettings(TextReader conf)
        {
        }

I'm trying to call the second constructor. 我正在尝试调用第二个构造函数。 TextReader is an abstract class, and TextReader是一个抽象类,并且

>>> QuickFix.SessionSettings(System.IO.StringReader("BLAH")) 

does not work, nor does: 不起作用,也不会:

>>> QuickFix.SessionSettings.Overloads[System.IO.TextReader](System.IO.StringReader("BLAH"))
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'type' object has no attribute 'Overloads'

I was able to call this by subclassing and implementing __new__ and __init__ , but is there a better way that doesn't require subclassing every multi-constructor type? 我可以通过子类化并实现__new____init__来调用它,但是有没有更好的方法不需要子类化每个多构造函数类型呢?

For future reference: 备查:

Suppose assembly A.dll references B.dll, and B.dll contains the target class to be subclassed. 假定程序集A.dll引用B.dll,并且B.dll包含要子类化的目标类。

clr.AddReferenceToFile("A.dll") # loads b.dll behind the scenes
clr.AddReferenceToFile("B.dll")

now the IronPython clr module implicitly has two references to B.dll, and this can cause absolutely cryptic errors such as the one above. 现在,IronPython clr模块隐式具有对B.dll的两个引用,这可能会导致绝对的密码错误,例如上述错误。

The solution seems to be to load only the minimal set of dlls, though I haven't seen much documentation on the exact mechanism of why this happens. 解决方案似乎是只加载最小数量的dll,尽管我还没有看到太多有关这种情况发生的确切机制的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM