Extending/Hacking Blue this morning

by Jason Haley 22. October 2005 03:32

This morning I “extended” Mike Stall's C# Compiler Blue to handle property short cuts modeled after C++/CLI's new syntax.  The code I wrote is a bit of a hack, but it gave me a chance to dig into a compiler and get a little better understanding (still have a long way to go to understand the big picture)...

So if all I want is a field exposed as a property, all I have to do it use syntax like this:

public property string Target2;

When the compiler comes across the 'property' token, it will now create code that looks like this in IL:

.field public string __Target2

.method public hidebysig specialname instance void
        set_Target2(string 'value') cil managed
{
  // Code size       8 (0x8)
  .maxstack  2
  IL_0000:  ldarg.0
  IL_0001:  ldarg.1
  IL_0002:  stfld      string BlueSample.Greeting::__Target2
  IL_0007:  ret
} // end of method Greeting::set_Target2

.method public hidebysig specialname instance string
        get_Target2() cil managed
{
  // Code size       16 (0x10)
  .maxstack  2
  .locals init (string V_0)
  IL_0000:  ldarg.0
  IL_0001:  ldfld      string BlueSample.Greeting::__Target2
  IL_0006:  stloc.s    V_0
  IL_0008:  br         IL_000d
  IL_000d:  ldloc.s    V_0
  IL_000f:  ret
} // end of method Greeting::get_Target2

Comments (0) | Post RSSRSS comment feed |

Categories:
Tags:

Comments are closed