c# - 这是有效的 C# 代码吗?

标签 c# .net compiler-construction decompiler

这是有效的 C# 代码吗?

  public class Product
  {
        [CompilerGenerated]
        private string <Name>k__BackingField;

    [CompilerGenerated]
    private decimal <Price>k__BackingField;

    public string Name
    {
     get;
     private set;
    }

    public decimal Price
    {
     get;
     private set;
    }

    public Product() 
        {
        }

    public static List<Product> GetSampleProducts()
    {
     List<Product> products = new List<Product>();
     Product num1.Price = new decimal(1233, 0, 0, false, 2).Add(num1);
     Product product1.Price = new decimal(1332, 0, 0, false, 2).Add(product1);
     Product num2.Price = new decimal(2343, 0, 0, false, 2).Add(num2);
     Product product2.Price = new decimal(2355, 0, 0, false, 2).Add(product2);
     return products;
    }

    public override string ToString()
    {
     return string.Format("{0}: {1}", this.Name, this.Price);
    }
   }

上面的示例取自 JustDecompile(一个 .NET 反编译器),您可以在下面看到原始版本:

using System;
using System.Collections.Generic;
using System.Text;

namespace ProductV3
{
   public class Product
   {
       public string Name { get; private set; }
       public decimal Price { get; private set; }

       public Product() { }

       public static List<Product> GetSampleProducts()
       {
          return new List<Product>()
          {
              new Product() { Name = "ProductA", Price = 12.33M },
              new Product() { Name = "ProductB", Price = 13.32M },
              new Product() { Name = "ProductC", Price = 23.43M },
              new Product() { Name = "ProductD", Price = 23.55M }
          };
       }

       public override string ToString()
       {
          return string.Format("{0}: {1}", Name, Price);
       }
   }
}

我想知道第一个 list 是反编译错误还是编译器生成的有效 C# 代码。我很好奇 GetSampleProducts 方法中的代码是什么。

最佳答案

通过将它粘贴到编译器并尝试编译它,很容易看出它不是有效的 C# 代码。

似乎反编译器不知道如何正确处理自动属性和对象字面量语法(不确定这是不是正确的术语)

关于 <Price>k__BackingField 的一件事这实际上是在 IL 中生成的支持字段的名称。 <>在 C# 中不是标识符名称的有效部分,但它们在 IL 中,这就是它们在编译自动属性时收到该名称的原因,因此它不会与您可能自己创建的任何变量名称冲突。自动属性只是编译器的魔法,毕竟它实际上确实在后台创建了一个私有(private)字段。

使用更完整的反编译器,您将获得更好的结果,例如,这就是 DotPeek 在反编译时给出的结果(甚至在删除调试符号的情况下进行了优化):

  public class Product
  {
    public string Name { get; private set; }

    public Decimal Price { get; private set; }

    public static List<Product> GetSampleProducts()
    {
      return new List<Product>()
      {
        new Product()
        {
          Name = "ProductA",
          Price = new Decimal(1233, 0, 0, false, (byte) 2)
        },
        new Product()
        {
          Name = "ProductB",
          Price = new Decimal(1332, 0, 0, false, (byte) 2)
        },
        new Product()
        {
          Name = "ProductC",
          Price = new Decimal(2343, 0, 0, false, (byte) 2)
        },
        new Product()
        {
          Name = "ProductD",
          Price = new Decimal(2355, 0, 0, false, (byte) 2)
        }
      };
    }

    public override string ToString()
    {
      return string.Format("{0}: {1}", (object) this.Name, (object) this.Price);
    }
  }

关于c# - 这是有效的 C# 代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6932782/

相关文章:

c# - Visual Studio 2015 花括号格式 : If, Else If, Else

c# - 如何将数字与自定义 2char BaseX 相互转换? (又名 : How to do Azure Table property compression)

c# - .Net Standard 2的96多个程序集复制到Console(.Net framework)Application的bin文件夹下

c# - 在 CheckedListBox 中显示友好名称

.net - NuGet 包版本和 NuGet Id

javascript - 我的 Scheme 编译器有什么问题?

c++ - 我如何打印到 LLVM 中的字符串

c# - 如何按元素列表分组?

c# - 多线程集合锁类型

c++ - 使用 Microsoft Visual C++ 2010 Express 时出现链接错误 LNK1123