3
votes

Is there an easy way to convert an IronPython file-like object to a .NET Stream? The reverse can be done in IronPython using code like this:

net_stream = File.OpenRead('file.txt')
python_file = file(net_stream)

Is there an easy way to convert it back? Or do you have to write a wrapper that will inherit from Stream and implement all the methods?

1
FWIW, I wanted to do the opposite, so your question code was exactly what I needed. - Michael

1 Answers

1
votes

apparently it's possible to do it like this:

import System
f = file('abc.txt', 'w+')
f.GetType().GetField('_stream', System.Reflection.BindingFlags.Instance | 
                                System.Reflection.BindingFlags.NonPublic).GetValue(f)