How to convert an Object to an Array? (W#)

4

I have the following class:

public string Nome;
public int Cpf;

 public string getNome() {        
    return Nome;         
}

 public void setNome(string nome) {
    this.Nome = nome;
}

public int getCpf() {
    return Cpf;
}

public void setCpf(int cpf) {
    this.Cpf = cpf;
}

How can I convert to an array?

I want to create a function for the database where I need to pass only the table name and a Array or OrderedDictionary with the mapping of this class, to avoid having to change the query each time I want to add a new attribute to it, I have this function in php and I'm trying to pass c# and the first thing I need to do is this conversion, in php I use the function implode() with array_keys() to separate the keys name of the fields in the database and the fields that will be inserted in the database. in the end I have a query so INSERT INTO {$table} ( {$fields} ) VALUES ( {$values})"; that serves to insert values in any table.

    
asked by anonymous 27.09.2015 / 23:01

3 answers

0

I decided instead to create a Array() as I was thinking I listed using Dictionary<string, string> . as follows:

//Cria uma bindingflag que armazena as propriedades da classe.
BindingFlags bindingFlags = BindingFlags.Public | 
    BindingFlags.NonPublic | 
    BindingFlags.Instance | 
    BindingFlags.Static;

Usuario user = new Usuario();

user.setNome("Something");
user.setCPF(123456789);

var newUser = new Dictionary<string, string>();

foreach (FieldInfo field in typeof(Usuario).GetFields(bindingFlags))
{
      print(field.Name + " Values => " + field.GetValue(user).ToString());
      newUser.Add(field.Name, field.GetValue(user).ToString());
}

Now I can build the querys dynamically as I do in php , and I no longer need to rewrite the querys since I can only list them automatically with the class itself, only care that the name of the variable must be the same name of the field in the table in the database.

    
30.09.2015 / 22:00
3

This is an extension that converts the properties of an object to an object dictionary with the property name as the key:

    public static IDictionary<string, object> ToDictionary(this object obj)
    {
        IDictionary<string, object> result = new Dictionary<string, object>();
        var properties = TypeDescriptor.GetProperties(obj);
        foreach (PropertyDescriptor property in properties)
        {
            result.Add(property.Name, property.GetValue(obj));
        }
        return result;
    }

From there you can convert the dictionary to an array, via [dicionario].Values.ToArray(); , for example.

    
30.09.2015 / 22:13
2

You need to use an ORM such as Entity Framework or Nhibernate because the purpose of ORM is to perform mapping between the fields / properties of your object and the fields / properties of your table in the database.

The ORM itself creates and executes the insert, update, etc. command. making it unnecessary for you to do this work manually.

Useful references:

NHibernate Site Source Code Entity Framework 6 (and earlier) Source Code Entity Framework 7

    
28.09.2015 / 00:19