3
In my code I use a class called GenericField<T>
which is used in all attributes of another class called Aplicacao
. Until everything well, however, in another part of the code I need to get via Reflection the name of a class-specific attribute Aplicacao
, which is actually an instance of GenericField<int>
.
I tried the code below but did not get the expected answer (the attribute name that would be "id", instead I got "Gerericfield1"), the implementation of the two classes is soon after.
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; }
}
}
Here:
MessageBox.Show(obj.id.GetType().Name.ToString());
you retrieve the name of the obj id property type name. This property has Genericfield type.– RSinohara
And how I get my name back?
– Benjamim Mendes Junior
Do you want the name of the property? It doesn’t make much sense. If you know what the property is, you know its name.
– Maniero
I want to create a function that will have as parameter a Genericfield and inside it I need to know the name of the attribute passed as parameter.
– Benjamim Mendes Junior