简体   繁体   中英

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

I'm trying to create a SessionState object using a TextReader. 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

>>> 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?

For future reference:

Suppose assembly A.dll references B.dll, and B.dll contains the target class to be subclassed.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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