Reflection of an Object that is owned by another

3

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; }
    }
}
    
asked by anonymous 07.07.2015 / 22:08

4 answers

0

If you are using C # 6, use the nameof keyword

As an example I suggest you follow the link below link

The nameof operator came along with C # 6 to make it easier to literally display a property name.

Example: nameof (object.Property)

    
08.07.2015 / 02:46
2

Now I understand what you need. I think the way you implement the classes is not possible.

Consider: The property (you want to retrieve the name) is GenericField . When a method receives this value, all it knows is about the variable (of type GenericField ). There is no way to know where it is referenced (as property of class Aplicacao , and maybe another place).

In your case this is reflected in your call obj.id. [etc.]. You use the property name. Of course, I imagine, your final code will not be like this (the property will be passed on, and will not be called by obj.{propriedade} itself).

An example of what you're wanting: you have a class A with a member named id and a called quantidade , both int. You pass both of them to a method (say calcular(int a, int b) , and you want to find within the method the name of those members within the A class.

The solution here would be to include a GenericField field in% w and a string propertyName field.     

08.07.2015 / 12:57
0

Hello, What you can do is also get the "DisplayName" attribute of the property.

Ex:

Extension method:

public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute
{
    var attrType = typeof(T);
    var property = instance.GetType().GetProperty(propertyName);
    return (T)property .GetCustomAttributes(attrType, false).First();
}

Code:

Aplicacao app = new Aplicacao();
var name = app.GetAttributeFrom<DisplayAttribute>("descricao").Name;

link

    
08.07.2015 / 14:29
-1

Please change the code for:

obj.GetType (). GetProperty ("id").

- Edit

static void Main(string[] args)
    {
        Aplicacao obj = new Aplicacao();


        Console.WriteLine(GetName(() => obj.id));
    }

    public static string GetName<T>(Expression<Func<T>> e)
    {
        var member = (MemberExpression)e.Body;
        return member.Member.Name;
    }
    
08.07.2015 / 03:34