简体   繁体   中英

Get object reference from PropertyInfo

I have a little problem, below my code:

public class A
{
    public string ObjectA { get; set; }
}

public void Run()
{
    A a = new A();
    a.ObjectA = "Test number 1";

    BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
    PropertyInfo myPropertyInfo = a.GetType().GetProperty("ObjectA", flags);
    object myNewObject = myPropertyInfo.GetValue(a);// Here should be reference

    a.ObjectA = "Test number 2";//After this line value myNewObject continued "Test number 1"
}

So my value myNewObject must be in output "Test number 2". Is there any way? It is this at all possible?

Wrong!

You're getting the string rather than the instance of A using reflection.

Changing A.ObjectA doesn't change the string reference. Actually, you're setting a different string to the backing string class field by the ObjectA property...

Auto-properties are syntactic sugar to avoid explicitly declaring class fields to properties which perform nothing when getting or setting their values:

// For example:
public string Text { get; set; }

// is...
private string _text;
public string Text { get { return _text; } set { _text = value; } }

Now turn your code into regular one (no reflection):

A a = new A();
a.ObjectA = "hello world";

object myNewObject = a.ObjectA;

// Do you think now that myNewObject should change...? :)
a.ObjectA = "goodbye";

Is there any way? It is this at all possible?

No.

Maybe you can simulate this behavior using a Func<T> ...

Func<object> myNewObjectGetter = () => myPropertyInfo.GetValue(a);

Now, whenever you call myNewObjectGetter() you're going to get the most fresh value of the whole property. BTW, this still doesn't address the impossible!

Is there any way?

No. You can't put a reference to a property into an object variable. Such a variable can only hold a normal value, such as the string you put into it.

That answers the question as asked. You can clarify what you want to achieve and maybe we can suggest a better way.

Probably there is no solution but I show some code

public class MyRows
{
    public string Key { get; set; }
    public int Id { get; set; }
    public object Val { get; set; }
}

public abstract class BasicDTO
{
    public int? Id { get; private set; }

    public PropertyInfo[] PropertyDTO;
    protected Type myType;

    public BasicDTO()
    {
        Load();
        BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
        PropertyDTO = myType.GetProperties(flags);
    }
}

public class CustomerDTO : BasicDTO
{
    public string Surname { get; set; }
    public string Phone { get; set; }

    public CustomerDTO() { }

    protected override void Load()
    {
        myType = typeof(CustomerDTO);         
    }
}

Now my basic method

public void Run(BasicDTO dto)
{
    PropertyInfo pi = dto.PropertyDTO.Where(x => x.Name == "Surname").SingleOrDefault();
    MyRows mr = new MyRows();
    mr.Val = pi.GetValue(dto);//Here I need reference
}

When I change CustomerDTO.Surname my value mr.Val it must also be changed. As I wrote above, it is probably impossible, but maybe anybody have a idea.

BTW: Value mr.Val I use only for binding (WPF). So maybe you have any other suggestions, how solve problem. I will be grateful for your help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM