简体   繁体   English

一种在构建时修改类(和常规装配)数据的方法

[英]A way to modify class (and general assembly) data at build time

This thing I want to do might not even be worth doing but I thought it would be cool. 我想做的事情可能甚至不值得,但我认为这很酷。

So what I want to do is to have some code that runs when my project is building (not only when compiling), and adds stuff to my classes based on things like attributes and general code analysis. 因此,我想做的是让一些代码在我的项目构建时(不仅在编译时)运行,并根据属性和常规代码分析之类的东西向我的类中添加内容。 What I want to do is have dynamically generated fields/properties that are usable through intellisense, but not visible in the actual source. 我想要做的是具有动态生成的字段/属性,这些字段/属性可通过智能感知使用,但在实际源中不可见。 The reason for that being that I might potentially want to generate a lot of them, and outputting them to source would turn into a mess very quickly. 之所以这样,是因为我可能要生成很多它们,然后将它们输出到源中会很快变得一团糟。

The potential possibilities of a system like that would be stuff like vector swizzling. 像这样的系统的潜在可能性可能是矢量旋转。

Is there maybe a library of some sort for that that I could just plug my generation code in? 也许有某种我可以插入我的生成代码的库吗? If not, what would be the best way to approach this, if there is any? 如果没有,那么解决这个问题的最佳方法是什么?

The most visible example of this is done by Microsoft for XAML files. Microsoft最明显的示例是XAML文件。 During the build, a C# source file is created for each XAML file and placed in the obj/Debug or obj/Release folder. 在生成期间,将为每个XAML文件创建一个C#源文件,并将其放置在obj/Debugobj/Release文件夹中。 In addition to that, the MSBuild .targets file where the relevant tasks are defined is specially configured to tell Visual Studio that the generated files are required for proper IntelliSense support, which means you don't actually have to explicitly build the project in order for IntelliSense to allow items declared in XAML to be used in C# code elsewhere in the project. 除此之外,特别定义配置了定义相关任务的MSBuild .targets文件,以告知Visual Studio生成的文件是正确的IntelliSense支持所必需的,这意味着您实际上不必显式构建项目即可IntelliSense允许XAML中声明的项目在项目其他地方的C#代码中使用。

This is exactly the method I use for generating code for ANTLR grammar files during a build. 正是我在构建期间用于为ANTLR语法文件生成代码的方法。 You can see a complete example with a build task assembly and custom .targets file here: 您可以在此处看到带有构建任务程序集和自定义.targets文件的完整示例:
https://github.com/antlr/antlrcs/tree/master/AntlrBuildTask https://github.com/antlr/antlrcs/tree/master/AntlrBuildTask

You should be aware that some 3 rd party extensions for Visual Studio completely replace the IntelliSense support with their own implementation of code completion. 你应该知道,为Visual Studio一些第三方的扩展完全取代自己的实现代码完成的IntelliSense支持。 Some of these extensions are known to not support the MSBuild IntelliSense extensibility features required for this to work with custom code generators. 已知其中某些扩展不支持MSBuild IntelliSense可扩展性功能,以便与自定义代码生成器一起使用。 If you run into problems with IntelliSense and have any extensions installed, you may find that removing the extensions completely resolves the problems. 如果遇到IntelliSense问题并安装了任何扩展程序,则可能会发现删除扩展程序可以完全解决问题。

应用程序运行时,应按类CSharpCodeProvider / ICodeCompiler / CompilerParameters编译代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 组装完成后,有什么方法可以删除类吗? - Is there any way to remove a class after an assembly has build? 在构建时缺少指令或汇编 - Missing directive or assembly on build time 修改插入以供一般使用 - Modify Insert for general use 对于Linq Select语句,如何在运行时以一般方式构造子类? - For Linq Select Statement, how to construct a SubClass in a general way at run time? 有没有简单的方法可以将类(TypeDefinition)导入其他程序集? - Is there an easy way to import a class(TypeDefinition) to other assembly? 有没有办法在运行时将类文件加载到程序集? - Is there a way to load a class file to assembly in runtime? 通常,是否有一种简单的方法(也许是一种工具)将程序集绑定版本与其清单定义进行匹配? - In general, is there an easy way (perhaps a tool) to match assembly binding versions with their manifest definitions? 有没有办法修改我的类对象的GC行为 - Is there a way to modify GC behavior for objects of my class 有没有办法在构建时注入代码? - Is there a way to inject code at build time? 获取运行时预编译的Azure Functions程序集的生成时间 - Get build time of Precompiled Azure Functions assembly during runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM