What is this "k_BackingField"

Asked

Viewed 279 times

5

I have a project to create a C# Smart Device for Windows CE. I received a code result reverse engineering, but there is a part that I am not able to understand or adapt. Below follows 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:

  1. What this k__BackingField would be. I did a search on this "tag/property" and saw things about Json, deserialization and WCF. Only that reading them, I believe that it does not apply to the purpose of the project.
  2. According to what I was given, the system is in C# and the code I have is actually in C#, but it has the statements of the strings and then in the following it comes the objects between "< >". What would that be? At the time I imagined that it was the declaration of Arrays, but in C# the symbol is [ ].

2 answers

6


The original code of this is:

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

I put in the Github for future reference.

I did reverse engineering of reverse engineering :)

This code at the lowest language level does not exist. Before processing to generate low-level CIL code you need to do a build process called Lowering, that is, it takes a more abstract syntax from low to a somewhat more concrete level, yet it remains in some abstraction, like everything except the excited electrons in the computer.

When this rewriting occurs it generates the code you obtained, that is, for each property has a private field and a couple of methods accessor and changer for this field. It uses a name that is guaranteed to be unique in the application by hurting the standard 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;
        }
    }
}

Like can be seen on Sharplab.

Already I answered about properties. Has links in more detail.

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

The use of <> there is only to ensure a syntax that will not conflict. These symbols are used in an appropriate way to Generics, but not in this case that it is something internal, you can not use this way in your code.

  • Got it! That already explains enough rs... I got lost was with the Generics tag. Thanks :D

3

Coming from reverse engineering, I am believing that the tool has not been able to identify the name of field (private) of property, or is arbitrarily defining a name by its own standard.

However, in addition to the form of writing not being very friendly, there is no secret. See 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 code written in a more 'natural' way, maybe we had something like this:

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

...

According to the C documentation# (in English), this code posted nor should compile (maybe intentionally), because variable names should:

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

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.