In my code I use a class called GenericField<T>
which is used on all attributes of another class named Aplicacao
. All the same, however, in another part of the code I need to get via reflection the name of a specific attribute of class Aplicacao
, which actually is an instance of GenericField<int>
.
I tried the code below but did not get the expected response (the name attribute would be "id", instead I got "GerericField1"), the implementation of the two classes is next.
Aplicacao obj = new Aplicacao();
MessageBox.Show(obj.id.GetType().Name.ToString());
Class GenericField
[Serializable]
public class GenericField<T>
{
private bool changeOldValue = true;
private T _Value;
public T Value
{
get { return _Value; }
set
{
if (changeOldValue)
_OldValue = value;
_Value = value;
changeOldValue = false;
}
}
private T _OldValue;
public T OldValue
{
get { return _OldValue; }
}
public override string ToString()
{
if (_Value == null)
return "";
return _Value.ToString();
}
}
Class Aplicacao
public class Aplicacao : Objeto_DTL
{
public Aplicacao()
{
_id = new GenericField<int>();
_nome = new GenericField<string>();
_descricao = new GenericField<string>();
_criacao = new GenericField<DateTime>();
}
public override string ToString() { return _id.ToString() + " - " + _nome.ToString(); }
private GenericField<int> _id;
[DisplayName("ID")]
public GenericField<int> id
{
get { return _id; }
set { _id = value; }
}
private GenericField<string> _nome;
[DisplayName("Nome")]
public GenericField<string> nome
{
get { return _nome; }
set { _nome = value; }
}
private GenericField<string> _descricao;
[DisplayName("Descrição")]
public GenericField<string> descricao
{
get { return _descricao; }
set { _descricao = value; }
}
private GenericField<DateTime> _criacao;
[DisplayName("Criação")]
public GenericField<DateTime> criacao
{
get { return _criacao; }
set { _criacao = value; }
}
}