Freigeben über


What if a C# snippet does not work when transliterated to IronPython?

Say you have a working piece of C# code. You want to include it in your IronPython app. So you transliterate it to IronPython code. And it stops working. What do you do?

You might feel lost at this point since the IronPython code is "supposed" to work. You have the following options:

  1. The first step ofcourse is to double-check your transliteration.

     

  2. The next step is simple but sometimes not obvious when you are in the middle of many other ongoing issues. This solution is to convert the C# code a function at a time to IronPython, or even a line at a time if needed. C# and IronPython can call each other very easily. So its easy to move small pieces of C# code over to IronPython. At every step, you check that the combined code works. You can use a binary search by speculatively moving bug chunks of code from C# to IronPython, and backing off when such a move exposes a problem. You will hopefully be able to narrow the problem down to a single line of IronPython code, which you can leave in C# until you figure out why IronPython cannot deal with it.

    This isnt a perfect solution. Some problems can be non-deterministic, and the problem might repro at random times. However, in many cases, you will be able to narrow down the problem to a single line of IronPython code.

  3. You could manually write C# code that uses Reflection.Emit to do the same work as the original C# code (once you have narrowed down the issue to a few line of code). IronPython code also gets compiled to Reflection.Emit code on the fly. Any issues that affect Reflection.Emit will be surfaced by your C# + Ref.Emit code.