C# 自动生成的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/491646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:28:36  来源:igfitidea点击:

auto-generated Code

c#visual-studio

提问by Random Developer

I am working on debugging some code and noticed a bunch of auto generated methods and objects.

我正在调试一些代码并注意到一堆自动生成的方法和对象。

At the top of the code for these I find the following comment:

在这些代码的顶部,我找到了以下注释:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.42
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

Howdo you figure out what generated the code? My curiosity has gotten the better of me on this so that is why I ask. I have looked for parts of the comment in Google and found nothing concrete.

怎么知道是什么生成了代码?我的好奇心在这方面占了上风,所以这就是我问的原因。我在谷歌中寻找了部分评论,但没有找到具体的内容。

采纳答案by Ferdeen

    //------------------------------------------------------------------------------ 
    // <auto-generated> 
    // This code was generated by a tool. 
    // Runtime Version:2.0.50727.42 
    // 
    // Changes to this file may cause incorrect behavior and will be lost if 
    // the code is regenerated. 
    // </auto-generated> 
    //------------------------------------------------------------------------------



namespace FirstWeb 
{ 
    public partial class _Default 
    { 

        /// <summary> 
        /// form1 control. 
        /// </summary> 
        /// <remarks> 
        /// Auto-generated field. 
        /// To modify move field declaration from designer file to code-behind file. 
        /// </remarks> 
        protected global::System.Web.UI.HtmlControls.HtmlForm form1; 
    } 
} 

You will not change this auto-generated C# file. As you keep adding ASP.NET controls to the page, this file grows with the additional declarations. In the older versions of C# (before version 2.0) and Visual Studio (before Version 2005), this code would be in the regular Default.aspx.cs file as well.

您不会更改此自动生成的 C# 文件。随着您不断向页面添加 ASP.NET 控件,此文件会随着附加声明的增加而增长。在旧版本的 C#(2.0 版之前)和 Visual Studio(2005 版之前)中,此代码也将位于常规 Default.aspx.cs 文件中。

With the introduction of partial classes in C#, the code belonging to the same class can be split across multiple files. Here you see the “public partial class _Default', which is used to hold the code generated by the Visual Studio designer. You will see the same class signature in the Default.aspx.cs file as well (you use this to write your own custom code).

随着 C# 中分部类的引入,属于同一个类的代码可以拆分到多个文件中。在这里您会看到“公共部分类 _Default”,它用于保存Visual Studio 设计器生成代码。您还将在 Default.aspx.cs 文件中看到相同的类签名(您可以使用它来编写您自己的自定义代码)。

So, the developer (you) and the designer (Visual Studio) can work independently without stepping over each other.

因此,开发人员(您)和设计人员(Visual Studio)可以独立工作而无需相互交叉。

This is taken from First Web Program (Web Project) in C# Explained

这是摘自C# 中的 First Web Program (Web Project) 解释

回答by Jon Skeet

It will entirely depend on what the code is.

这将完全取决于代码是什么。

Presumably you know the class you're debugging - whether it's part of an ORM, part of a web service generated proxy etc. That's the crucial bit of information.

大概你知道你正在调试的类 - 无论它是 ORM 的一部分,还是 Web 服务生成代理的一部分等等。这是关键的信息。

回答by Marc Gravell

Well, what code is it? Most of the MS tools include this header. xsd.exedoes, sqlmetal(LINQ-to-SQL) does, etc. Try looking down a few lines... for example, a quick xsd test for me shows a second comment block:

嗯,是什么代码?大多数 MS 工具都包含此标头。xsd.exe做,sqlmetal(LINQ-to-SQL)做,等等。试着往下看几行……例如,对我来说一个快速的 xsd 测试显示了第二个注释块:

// 
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
// 

But this doesn't exist in the LINQ-to-SQL code... ;-(

但这在 LINQ-to-SQL 代码中不存在...;-(

回答by Ian Varley

Looks like Visual Studio to me. Try Googling only part of the message.

对我来说看起来像 Visual Studio。尝试只搜索消息的一部分

回答by Ian Varley

I think this code was generated by the Linq to SQL engine.

我认为这段代码是由 Linq to SQL 引擎生成的。

you can modify it if you understand how to code with Sql Enities

如果您了解如何使用 Sql Enities 进行编码,则可以对其进行修改

回答by Anton Gogolev

This can also be a .Designer.cs file generated by ResXFileCodeGenerator, which generates strongly-typed wrappers for .resx files.

这也可以是由 ResXFileCodeGenerator 生成的 .Designer.cs 文件,它为 .resx 文件生成强类型包装器。

回答by Rich

These comments exactly match those found in some of my service reference files. These are generated proxy files for WCF services, so perhaps your code is also in this area.

这些注释与我的一些服务参考文件中的注释完全匹配。这些是为 WCF 服务生成的代理文件,因此您的代码可能也在此区域。

If you can't see those files in Solution Explorer, click 'Show All Files' and they will appear as .cs files in the Service references node.

如果您在解决方案资源管理器中看不到这些文件,请单击“显示所有文件”,它们将在服务引用节点中显示为 .cs 文件。

回答by dlsou

This is the exact comment that is generated by System.CodeDom when generating a file from an entire code compile unit.

这是 System.CodeDom 在从整个代码编译单元生成文件时生成的确切注释。

回答by user1435666

The SVCUTIL tool from Microsoft (http://msdn.microsoft.com/en-us/library/aa347733.aspx) will generate source code files with this comment at the top of each file without identifying itself as the tool that generated the file.

Microsoft 的 SVCUTIL 工具 ( http://msdn.microsoft.com/en-us/library/aa347733.aspx) 将在每个文件的顶部生成带有此注释的源代码文件,而不会将其标识为生成文件的工具.