What would this "k_BackingField"

5

I'm having a project to create a Smart Device in C # for Windows CE. I received a reverse engineering result code, but it has a part that I can not understand or adapt. Here is the code:

private string <codProduto>k__BackingField;
private string <funcionario>k__BackingField;
private string <estante>k__BackingField;
private string <armazem>k__BackingField;
private string <filial>k__BackingField;

public string codProduto
{
  get
  {
    return this.<codProduto>k__BackingField;
  }
  set
  {
    this.<codProduto>k__BackingField = value;
  }
}

public string funcionario
{
  get
  {
    return this.<funcionario>k__BackingField;
  }
  set
  {
    this.<funcionario>k__BackingField = value;
  }
}

public string estante
{
  get
  {
    return this.<estante>k__BackingField;
  }
  set
  {
    this.<estante>k__BackingField = value;
  }
}

public string armazem
{
  get
  {
    return this.<armazem>k__BackingField;
  }
  set
  {
    this.<armazem>k__BackingField = value;
  }
}

public string filial
{
  get
  {
    return this.<filial>k__BackingField;
  }
  set
  {
    this.<filial>k__BackingField = value;
  }
}

I have two questions:

  • What would this k__BackingField be? I did a search on this "tag / property" and saw things about JSon, deserialization and WCF. Only by reading them, I believe it does not apply to the purpose of the project.
  • The system is in C # and the code I have it is actually in C #, but it has the string declarations, and then comes the objects between "< >". What would that be? At the time I imagined it to be the Arrays statement, but in C # the symbol is [].
  • asked by anonymous 14.06.2018 / 06:00

    2 answers

    6

    The original code for this is:

    public class C {
        public string estante { get; set; }
        public string armazem { get; set; }
        public string filial { get; set; }
    }
    

    I have reverse-engineered reverse engineering:)

    This code at the lowest level of the language does not exist. Before processing to generate the low-level CIL code you have to do a compilation process called lowering , that is, it takes a more abstract lower syntax to a slightly more concrete level, even though remains in some abstraction, like everything except the excited electrons in the computer.

    When this rewrite occurs it generates the code you got, ie for each property has a private field and a couple of accessor and mutator methods for this field. It uses a name that is guaranteed to be unique in the application by hurting the default syntax allowed in the language.

    The code should look like this:

    public class C {
        [CompilerGenerated]
        private string <estante>k__BackingField;
        [CompilerGenerated]
        private string <armazem>k__BackingField;
        [CompilerGenerated]
        private string <filial>k__BackingField;
    
        public string estante {
            [CompilerGenerated]
            get {
                return <estante>k__BackingField;
            }
            [CompilerGenerated]
            set {
                <estante>k__BackingField = value;
            }
        }
    
        public string armazem {
            [CompilerGenerated]
            get {
                return <armazem>k__BackingField;
            }
            [CompilerGenerated]
            set {
                <armazem>k__BackingField = value;
            }
        }
    
        public string filial {
            [CompilerGenerated]
            get {
                return <filial>k__BackingField;
            }
            [CompilerGenerated]
            set {
                <filial>k__BackingField = value;
            }
        }
    }
    

    As can be viewed in SharpLab .

    I already have answered properties . It has links with more details.

    What you found about JSON and WCF was circumstantial. Nothing to see directly.

    Using <> is just to ensure syntax that will not conflict. These symbols are appropriately used for generics , but not in this case which is something internal, you can not use this way in your code.

        
    14.06.2018 / 07:45
    3

    Coming from reverse engineering, I'm assuming the tool was not able to identify the field (private) name of the property , or is arbitrarily setting a name standard itself.

    However, in addition to writing is not very friendly, there is no secret. Look at this isolated case, for example:

    private string <codProduto>k__BackingField;
    
    ...
    
    public string codProduto
    {
      get
      {
        return this.<codProduto>k__BackingField;
      }
      set
      {
        this.<codProduto>k__BackingField = value;
      }
    }
    

    In a more 'natural' written code, we might have something like this:

    private string codProduto;
    public string CodProduto
    {
      get { return codProduto; }
      set { codProduto = value; }
    }
    
    ...
    

    Second

    • Begin with a letter, underline ( _ ) or at ( @ );
    • Have the remaining characters within the set of letters, numbers or underline ( _ ).

    I hope I have helped.

        
  • 14.06.2018 / 06:30