简体   繁体   English

在编译时从C#获取数据

[英]Get data from in compile time C#

I've been trying to write C# code that retrieves data from a file in compilation-time, and not in run-time. 我一直在尝试编写C#代码,以便在编译时而不是在运行时从文件中检索数据。 The thing is that I need to get a version of a file that exists in my machine, but not in machines that would run this DLL. 问题是我需要获取计算机中存在的文件的版本,而不是将运行此DLL的计算机中存在的文件的版本。 (The version can be changed tomorrow, so I want to able to retrieve that dynamically). (该版本明天可以更改,因此我希望能够动态检索该版本)。 In run-time, those machines have no access to the file, and that's why I need to get this data before. 在运行时,这些计算机无法访问该文件,这就是为什么我需要先获取此数据。 Anyone as an idea how to do so ? 任何人都想怎么做? Thanks. 谢谢。

The C# code that you are compiling will not be run during the compilation. 您正在编译的C#代码将不会在编译过程中运行。

You should make a pre-build step that extracts the version information. 您应该执行一个预构建步骤来提取版本信息。 That custom build step could in itself be a C# program if you want. 如果需要,该自定义生成步骤本身可以是C#程序。 Make that program output a small .cs file containing the version number of the dll and include that .cs file in the source for the main program. 使该程序输出一个包含dll版本号的.cs小文件,并将该.cs文件包含在主程序的源代码中。

如果要在构建项目之前或之后执行某些操作,可以在项目属性的“ Build Events部分的“ Build Events pre-build event command line或“ post-build command line中编写命令行语句。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM