C#中ValueTuple的原理详解

2020-01-05 09:31:38王旭

先来写一段代码,编译之后对他反编译,看一下他是怎么做的


  static void Main(string[] args)
  {
   var foo = Foo();
   var str = JsonConvert.SerializeObject(foo);
   Console.WriteLine(str);
  }

  static (string name, string site) Foo()
  {
   return (name: "lindexi", site: "blog.csdn.net/lindexi_gd");
  }

不需要安装反编译软件,可以使用这个网站拿到反编译

可以看到Foo被编译为 TupleElementNames 特性的两个字符串


 [return: TupleElementNames(new string[]
 {
  "name",
  "site"
 })]
 private static ValueTuple<string, string> Foo()
 {
  return new ValueTuple<string, string>("lindexi", "blog.csdn.net/lindexi_gd");
 }

所以实际上代码是 ValueTuple<string, string> 不是刚才定义的代码,只是通过 TupleElementNames 让编译器知道值,所以是语法糖。

IL 代码是


private hidebysig static valuetype [mscorlib]System.ValueTuple`2<string, string> 
 Foo() cil managed 
 {
 .param [0] 
 .custom instance void [mscorlib]System.Runtime.CompilerServices.TupleElementNamesAttribute::.ctor(string[]) 
  = (
  01 00 02 00 00 00 04 6e 61 6d 65 04 73 69 74 65 // .......name.site 这里就是 return: TupleElementNames 的命名
  00 00           // ..
  )
 .maxstack 2
 .locals init (
  [0] valuetype [mscorlib]System.ValueTuple`2<string, string> V_0
 )

 // [20 9 - 20 10]
 IL_0000: nop   

 // [21 13 - 21 72]
 IL_0001: ldstr  "lindexi"
 IL_0006: ldstr  "blog.csdn.net/lindexi_gd"
 IL_000b: newobj  instance void valuetype [mscorlib]System.ValueTuple`2<string, string>::.ctor(!0/*string*/, !1/*string*/)
 IL_0010: stloc.0  // V_0
 IL_0011: br.s   IL_0013

 // [22 9 - 22 10]
 IL_0013: ldloc.0  // V_0
 IL_0014: ret   
 }

这个特性只有编译器可以用,不可以在代码使用。

在上面的解释,实际上 IL 不知道存在定义的命名,所以不可以通过这个方法获得值。