gpt4 book ai didi

c# - 生成的 C# 代码是否有任何 #pragma 或类似指令将模板代码行号与 C# 行号匹配?

转载 作者:太空狗 更新时间:2023-10-29 18:15:42 24 4
gpt4 key购买 nike

我有一个看起来类似于旧式 ASP 代码的模板系统。我通过一个类来运行它,该类将整个内容重写为 C# 源代码、编译并最终执行它。

我想知道是否有某种#pragma-like 指令我可以在生成的 C# 代码中使用它来使编译错误与我的模板文件中的行号相匹配?

例如,假设我的模板代码中有第一行也是唯一一行:

Object o = datta; // should be data, compiler error

但是为了编译它,我必须向它添加一个命名空间、一个类、一个方法和一些样板代码,所以上面这一行,也就是我模板文件中的第一行,实际上最终是C# 代码中的第 17 行(随机数,仅用于说明目的)。编译器错误自然会将我的错误标记为第 17 行,而不是第 1 行。

我记得我以前使用过另一种编程语言(虽然我不记得是哪一种),它有一个我可以添加的指令,这将使错误行号对齐。

C# 3.5 中有这样的东西吗?

最佳答案

你有 #line preprocessor directive .

#line lets you modify the compiler's line number and (optionally) the file name output for errors and warnings.

The #line directive might be used in an automated, intermediate step in the build process. For example, if lines were removed from the original source code file, but you still wanted the compiler to generate output based on the original line numbering in the file, you could remove lines and then simulate the original line numbering with #line.

The #line hidden directive hides the successive lines from the debugger, such that when the developer steps through the code, any lines between a #line hidden and the next #line directive (assuming that it is not another #line hidden directive) will be stepped over. This option can also be used to allow ASP.NET to differentiate between user-defined and machine-generated code. Although ASP.NET is the primary consumer of this feature, it is likely that more source generators will make use of it.

A #line hidden directive does not affect file names or line numbers in error reporting. That is, if an error is encountered in a hidden block, the compiler will report the current file name and line number of the error.

The #line filename directive specifies the file name you want to appear in the compiler output. By default, the actual name of the source code file is used. The file name must be in double quotation marks ("") and must be preceded by a line number.

关于c# - 生成的 C# 代码是否有任何 #pragma 或类似指令将模板代码行号与 C# 行号匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1358487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com