問題描述
Visual Studio 的 Python 代碼生成器? (Python code generator for Visual Studio?)
我有一個想法,如果我將 python .py 文件添加到我的 C# 項目中,並使用將執行 python 文件的自定義生成器標記該文件,並將輸出視為代碼生成的結果,即。將其放入 C# 文件中,這樣我就可以在構建過程中生成大量代碼。
有人知道是否存在這樣的 Visual Studio 2008 自定義生成器嗎?
參考解法
方法 1:
I think Cog does what you want.
方法 2:
I recall that in previous versions of VS, there was a way to add custom build steps to the build process. I used that a lot to do exactly the kind of automated code generation you describe.
I imagine the custom build step feature is still there in 2008.
方法 3:
I don't understand what you are trying to do here. Are you trying to execute a Python script that generates a C# file and then compile that with the project? Or are you trying to compile a Python script to C#?
方法 4:
OK, I see. Well, as far as I know there isn't any code generator for Python. There is a good introduction on how to roll your own here.
Actually, that's quite an under‑used part of the environment, I suppose it's so because it needs you to use the IDE to compile the project, as it'd seem only the IDE knows about these "generators", but MSBuild ignores them.
方法 5:
I dug through my old bookmarks (I love Del.icio.us!) and found this article: Code Generation with Python, Cog, and Nant. Keep in mind that anything you can do in NAnt can probably be done in MSBuild as well. This should be enough to get you started.
(by Lasse V. Karlsen、jwfearn、Greg Hewgill、dguaraglia、dguaraglia、technomalogical)