Search code examples
pythonc#clr

Is there a way to tie my C# methods to my Python scripts and check if they are valid?


A lot of my Python scripts interface with my C# code and when I make changes to my C# code, it breaks a ton of my Python scripts. I am running Visual Studio for all of my code and was wondering if there is a way to catch this during compilation? It's annoying to push changes and then someone complains that the script has broken because I changed my type or method header. Right now, I do a check where I make sure the method exists within the code before executing in Python but it would be nice to take one step further and have it be checked before I even push the code. We log an error when the method doesn't exist/has changed but having it taken a step further would be the goal.


Solution

  • There is no way to do this out-of-the-box. But there is a way I used in my career:

    1. Make C# API library (DTO, interfaces), without dependencies.
    2. On pre-compilation stage, generate Python hooks for those interfaces. Should be pretty straight-forward using reflection.
    3. Make some Python unit-tests, mocking C# interfaces, this will ensure integrity.