1
I am developing an application descktop in c# The framework 4.5
I did not imagine that the application could grow and have many fields, with this came the need to make an automatic mapping (from / to) of the controls to entities...
Example.
public Pessoa GetPessoaForm()
{
//Obtém todos os controles de um formulário
IEnumerable<Control> controles = frm.GetAll();
Pessoa pessoa = new Pessoa();
var prop = typeof(pessoa).GetProperties();
foreach ( Controle control in controles)
{
foreach (var p in prop)
{
If(!p.Name.ToLower().Equals(control.Tag.ToString().ToLower())
continue;
If (typeof(control).Equals(typeof(TextBox))
Control.Text = p.GetValue(prop, null); //erro também
}
}
}
This way I would like to map to the form and vice versa. From the Entity Form.
What error does it give you when you try to assign the value?
– João Martins
Null object, maybe you are trying to rescue the property wrong, there is some framework to do this kind of automatic mapping?
– Luiz Silva
Search MVVM or MVP frameworks for Winforms. I don’t know if you have anything good available...
– Jéf Bueno
The problem should be the time and place at which this method is called. Usually the controls are created only when the form is initialized. In addition, if the form contains panels or other types of 'containers', you will have to create a recursion in this loop that iterates the form controls in order to check all your child controls.
– Henrique
@Linq good dirty, if demanded in the web world, maybe there is in the world desk
– Luiz Silva